Title: Issue using from_orm stripping Relationship data from models · Issue #224 · fastapi/sqlmodel · GitHub
Open Graph Title: Issue using from_orm stripping Relationship data from models · Issue #224 · fastapi/sqlmodel
X Title: Issue using from_orm stripping Relationship data from models · Issue #224 · 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/224
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Issue using from_orm stripping Relationship data from models","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\nfrom sqlmodel import SQLModel, Relationship, Field, create_engine, Session\r\nfrom typing import List, Optional, Literal\r\n\r\nengine = create_engine(\"sqlite://\", connect_args={\"check_same_thread\": False})\r\n\r\n\r\ndef create_db_and_tables():\r\n SQLModel.metadata.create_all(engine)\r\n\r\n\r\nclass LocationBase(SQLModel):\r\n street: str\r\n city: str\r\n country: str\r\n\r\n\r\nclass Location(SQLModel, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n\r\n employee_id: Optional[int] = Field(default=None, foreign_key=\"employee.id\")\r\n employee: \"Employee\" = Relationship(back_populates='locations')\r\n\r\n\r\nclass EmployeeBase(SQLModel):\r\n name: str\r\n job: str\r\n\r\n\r\nclass Employee(EmployeeBase, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n locations: List[Location] = Relationship(back_populates=\"employee\")\r\n\r\n\r\nclass EmployeeCreate(EmployeeBase):\r\n locations: List[Location]\r\n\r\n\r\ndef create_employees():\r\n locations = [Location(street='State St.', city='New York', country='USA')]\r\n e = EmployeeCreate(name='Test Employee', job='Test Job', locations=locations)\r\n\r\n employee = Employee.from_orm(e) # this strips the locations list from the Employee object\r\n print(employee)\r\n\r\n # You have to manually readd the locations before sending data to the DB.\r\n # If you skip this line then no location data will be added to the DB.\r\n e.locations = locations\r\n\r\n # Now you can commit.\r\n\r\n\r\ndef main():\r\n create_db_and_tables()\r\n create_employees()\r\n\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\n\r\n### Description\r\n\r\nApologies if this seems trivial! When using SQLModel's `from_orm` method the relationship data is stripped from the outputted model. I think that there may be an issue with how the `from_orm` method works in SQLModel but I can't diagnose where the data is actually stripped.\r\n\r\nI found a similar issue here: https://github.com/samuelcolvin/pydantic/issues/1334. The difference is that typically (before SQLModel) most models refer to other Pydantic models instead of other relationships. This could be an example of a typical model relationship in Pydantic:\r\n\r\n```\r\nfrom pydantic import BaseModel\r\n\r\n\r\nclass Employee(BaseModel):\r\n id: int\r\n name: str\r\n type: str\r\n\r\n\r\nclass Address(BaseModel):\r\n id: int\r\n city: str\r\n employee: Employee\r\n\r\n class Config:\r\n orm_mode = True\r\n\r\ne = Employee(id=1, name='Test', type='pydantic')\r\na= Address(id=1, city='New York', employee=e)\r\n\r\nprint(a.from_orm(a))\r\n```\r\n\r\n(This correctly outputs `id=1 city='New York' employee=Employee(id=1, name='Test', type='pydantic')`)\r\n\r\nIt's not a big deal to readd the models but it becomes tedious if you have many models to manage. I was wondering if anyone else had this issue and I couldn't find it referenced anywhere else. Thank you so much!\r\n\r\n### Operating System\r\n\r\nmacOS\r\n\r\n### Operating System Details\r\n\r\n_No response_\r\n\r\n### SQLModel Version\r\n\r\n0.0.6\r\n\r\n### Python Version\r\n\r\n3.8.1\r\n\r\n### Additional Context\r\n\r\n_No response_","author":{"url":"https://github.com/res234","@type":"Person","name":"res234"},"datePublished":"2022-01-15T08:33:21Z","url":"https://github.com/fastapi/sqlmodel/issues/224"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:6b68ccac-9ad3-aa3f-da6d-442af549b4b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BFDA:284967:1F12DA:2A489E:6AFE18AA |
| html-safe-nonce | 435bc0dc7a1900de914f67aaf47aa9dd641903f290e4776b715c3857161ad032 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkRBOjI4NDk2NzoxRjEyREE6MkE0ODlFOjZBRkUxOEFBIiwidmlzaXRvcl9pZCI6IjY4NTkxMDgzNTE2OTk5ODI1MDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 94420d21fb281ed8df7497cc7fcc258b575655f268d691a1179029f221ef6f92 |
| hovercard-subject-tag | issue:1104639543 |
| 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/224/issue_layout |
| twitter:image | https://opengraph.githubassets.com/13fa83796e018b521c1c68c01a33bba156818cf82384fc82c42a224f9d09bfcf/fastapi/sqlmodel/issues/224 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/13fa83796e018b521c1c68c01a33bba156818cf82384fc82c42a224f9d09bfcf/fastapi/sqlmodel/issues/224 |
| 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 | res234 |
| hostname | github.com |
| expected-hostname | github.com |
| None | d0525ab1464526bf4532a5551aa348cf100dff75ef808a96fc67a08b1d946aa9 |
| 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 | fef3993a71fa20f7be8e167132fbf9619bc66e12 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width