Title: Is it possible to instantiate a SQLModel object with relationships from a `dict` type? · Issue #342 · fastapi/sqlmodel · GitHub
Open Graph Title: Is it possible to instantiate a SQLModel object with relationships from a `dict` type? · Issue #342 · fastapi/sqlmodel
X Title: Is it possible to instantiate a SQLModel object with relationships from a `dict` type? · Issue #342 · 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/342
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Is it possible to instantiate a SQLModel object with relationships from a `dict` type?","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 typing import List, Optional\r\n\r\nfrom sqlmodel import Field, Relationship, Session, SQLModel, create_engine\r\n\r\nfrom pydantic import BaseModel\r\n\r\n\r\nclass Team(SQLModel, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n\r\n heroes: List[\"Hero\"] = Relationship(back_populates=\"team\")\r\n\r\n\r\nclass Hero(SQLModel, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n\r\n team_id: Optional[int] = Field(default=None, foreign_key=\"team.id\")\r\n team: Optional[Team] = Relationship(back_populates=\"heroes\")\r\n\r\n\r\nclass Hero2(BaseModel):\r\n id: int\r\n team: Team\r\n\r\n\r\nclass Team2(BaseModel):\r\n id: int\r\n heroes: List[\"Hero2\"]\r\n\r\n# What I would like to be able to do\r\nd = {\"id\": 123, \"team\": {\"id\": 124}} # fails\r\n# d = {\"id\": 123, \"team\": Team(**{\"id\": 124})} # succeeds\r\nh = Hero(**d)\r\nprint(h)\r\n\r\n# The same idea but using vanilla Pydantic models\r\nd2 = {\"id\": 123, \"team\": {\"id\": 124}}\r\nh2 = Hero2(**d2)\r\nprint(h2)\r\n```\r\n\r\n\r\n### Description\r\n\r\nIn Pydantic it is possible to instantiate objects directly from dicts (i.e. JSON) via `ClassName(**dict)`. This also works for objects with nested objects (i.e. relationships in SQLModel). Is it possible to do the same in SQLModel? I would like to take a JSON like `{\"id\": 123, \"relationship_obj\": {\"id\": 456, ...}}` and have SQLModel correctly create the relationship model based on the type of the field & the key of the dict passed in.\r\n\r\nThe error received is:\r\n```\r\n File \"/.../venv/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py\", lin\r\ne 1729, in emit_backref_from_scalar_set_event\r\n instance_state(child),\r\nAttributeError: 'dict' object has no attribute '_sa_instance_state'\r\n```\r\n\r\n### Operating System\r\n\r\nLinux\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\nPython 3.9.5\r\n\r\n### Additional Context\r\n\r\n_No response_","author":{"url":"https://github.com/JLHasson","@type":"Person","name":"JLHasson"},"datePublished":"2022-05-17T04:29:36Z","url":"https://github.com/fastapi/sqlmodel/issues/342"}
| 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:bea47a10-afce-e77e-57f6-8730293d9bd0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DDDC:37BBB5:8BC0E:BAEEA:6AFE1F1F |
| html-safe-nonce | 20f2db4c7c934c65acfd1c0b051c6c77158befb9b16523b0627549ae7a3b86f0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERERDOjM3QkJCNTo4QkMwRTpCQUVFQTo2QUZFMUYxRiIsInZpc2l0b3JfaWQiOiIxNDcwNTcyNDYwMDcwODA1Mjc5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9882540c2e7789cca6a07456063f55de6ad68c7d12c7091aaaed71fa83afcc61 |
| hovercard-subject-tag | issue:1238048608 |
| 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/342/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2cc304a3e5734f33b9e87e6517d354c02548aaef86fea2a5860cc129b85d2bdc/fastapi/sqlmodel/issues/342 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2cc304a3e5734f33b9e87e6517d354c02548aaef86fea2a5860cc129b85d2bdc/fastapi/sqlmodel/issues/342 |
| 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 | JLHasson |
| 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