Title: Example: Understanding inheritance and relationships between model classes · Issue #488 · fastapi/sqlmodel · GitHub
Open Graph Title: Example: Understanding inheritance and relationships between model classes · Issue #488 · fastapi/sqlmodel
X Title: Example: Understanding inheritance and relationships between model classes · Issue #488 · fastapi/sqlmodel
Description: First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I already searched in Google "How to X in...
Open Graph Description: First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I...
X Description: First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated searc...
Opengraph URL: https://github.com/fastapi/sqlmodel/issues/488
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Example: Understanding inheritance and relationships between model classes","articleBody":"### First Check\r\n\r\n- [X] I added a very descriptive title to this issue.\r\n- [X] I used the GitHub search to find a similar issue and didn't find it.\r\n- [X] I searched the SQLModel documentation, with the integrated search.\r\n- [X] I already searched in Google \"How to X in SQLModel\" and didn't find any information.\r\n- [X] I already read and followed all the tutorial in the docs and didn't find an answer.\r\n- [X] I already checked if it is not related to SQLModel but to [Pydantic](https://github.com/samuelcolvin/pydantic).\r\n- [X] I already checked if it is not related to SQLModel but to [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy).\r\n\r\n### Commit to Help\r\n\r\n- [X] I commit to help with one of those options 👆\r\n\r\n### Example Code\r\n\r\n```python\r\nclass Contract(SQLModel):\r\n \"\"\"A contract defines the business conditions of a project\"\"\"\r\n title: str = Field(description=\"Short description of the contract.\")\r\n client: Client = Relationship(\r\n back_populates=\"contracts\",\r\n )\r\n # Contract n:1 Client\r\n client_id: Optional[int] = Field(\r\n default=None,\r\n foreign_key=\"client.id\",\r\n )\r\n\r\n currency: str \r\n term_of_payment: Optional[int] = Field(\r\n description=\"How many days after receipt of invoice this invoice is due.\",\r\n default=31,\r\n )\r\n\r\n \r\nclass TimeContract(Contract, table=True):\r\n \"\"\"A time-based contract with a rate per time unit\"\"\"\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n\r\n rate: condecimal(decimal_places=2) = Field(\r\n description=\"Rate of remuneration\",\r\n )\r\n\r\n unit: TimeUnit = Field(\r\n description=\"Unit of time tracked. The rate applies to this unit.\",\r\n sa_column=sqlalchemy.Column(sqlalchemy.Enum(TimeUnit)),\r\n default=TimeUnit.hour,\r\n )\r\n\r\nclass WorksContract(Contract, table=True):\r\n \"\"\"A contract with a fixed price\"\"\"\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n price: condecimal(decimal_places=2) = Field(\r\n description=\"Price of the contract\",\r\n )\r\n deliverable: str = Field(description=\"Description of the deliverable\")\r\n\r\nclass Client(SQLModel, table=True):\r\n \"\"\"A client the freelancer has contracted with.\"\"\"\r\n\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n name: str\r\n contracts: List[\"Contract\"] = Relationship(back_populates=\"client\")\r\n```\r\n\r\n\r\n### Description\r\n\r\nI would like to understand inheritance better using the following example:\r\n- There are two types of contracts, a time-based contract with a rate per time unit, and a works contract with a fixed price.\r\n- Every contract is related to a client.\r\n\r\nThe code example is my first attempt at implementing this. However, It is not yet correct:\r\n\r\n```\r\nInvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Client-\u003eclient'. Original exception was: When initializing mapper mapped class Client-\u003eclient, expression 'Contract' failed to locate a name ('Contract'). If this is a class name, consider adding this relationship() to the \u003cclass 'tuttle.model.Client'\u003e class after both dependent classes have been defined.\r\n```\r\n\r\nQuestions:\r\n\r\n- Every `Contract` is related to a client. That makes me think `client` should be a member of the `Contract` base class. However, I don't understand how to properly implement the relationship. Is it necessary to move `client` to the table classes (thereby duplicating it)?\r\n- When using inheritance from a model class, can I still `select` from `Contract` (rather than from the different contract types separately)?\r\n\r\n### Operating System\r\n\r\nmacOS, Other\r\n\r\n### Operating System Details\r\n\r\n_No response_\r\n\r\n### SQLModel Version\r\n\r\n0.0.8\r\n\r\n### Python Version\r\n\r\n3.10\r\n\r\n### Additional Context\r\n\r\n_No response_","author":{"url":"https://github.com/clstaudt","@type":"Person","name":"clstaudt"},"datePublished":"2022-11-05T08:48:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":13},"url":"https://github.com/488/sqlmodel/issues/488"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:03967edf-5c48-a04d-c92a-8a7bcdf8a441 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9940:12D78F:2D949E4:3E7A4EA:6964E758 |
| html-safe-nonce | 6d264fa85b344d30d663fe2730f82ee9b497c5593501e2ef54b017ed7ccfab69 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTQwOjEyRDc4RjoyRDk0OUU0OjNFN0E0RUE6Njk2NEU3NTgiLCJ2aXNpdG9yX2lkIjoiMzgxNzQzMjYzODg2MjcxNDcxMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 73660945a771d4ba68645c8562fb1cee3c8c8565f4ea3ebf54ce19f3e1e104df |
| hovercard-subject-tag | issue:1436907185 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/fastapi/sqlmodel/488/issue_layout |
| twitter:image | https://opengraph.githubassets.com/785eb2700869614ab1bd35bc8c6b62563cb1c2de07180e4ca8276a3e0dc51f75/fastapi/sqlmodel/issues/488 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/785eb2700869614ab1bd35bc8c6b62563cb1c2de07180e4ca8276a3e0dc51f75/fastapi/sqlmodel/issues/488 |
| og:image:alt | First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | clstaudt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3c30097417ecb9dfdab6b2e8bc7dc38d73e183d6ad48f94cb69e4a5daa2dbb87 |
| turbo-cache-control | no-preview |
| go-import | github.com/fastapi/sqlmodel git https://github.com/fastapi/sqlmodel.git |
| octolytics-dimension-user_id | 156354296 |
| octolytics-dimension-user_login | fastapi |
| octolytics-dimension-repository_id | 399495186 |
| octolytics-dimension-repository_nwo | fastapi/sqlmodel |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 399495186 |
| octolytics-dimension-repository_network_root_nwo | fastapi/sqlmodel |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 69fc54a84c74307369dba42af5401200531d116e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width