Title: Not able to read data via relationship back-populates · Issue #383 · fastapi/sqlmodel · GitHub
Open Graph Title: Not able to read data via relationship back-populates · Issue #383 · fastapi/sqlmodel
X Title: Not able to read data via relationship back-populates · Issue #383 · 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/383
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Not able to read data via relationship back-populates","articleBody":"### First Check\n\n- [X] I added a very descriptive title to this issue.\n- [X] I used the GitHub search to find a similar issue and didn't find it.\n- [X] I searched the SQLModel documentation, with the integrated search.\n- [X] I already searched in Google \"How to X in SQLModel\" and didn't find any information.\n- [X] I already read and followed all the tutorial in the docs and didn't find an answer.\n- [X] I already checked if it is not related to SQLModel but to [Pydantic](https://github.com/samuelcolvin/pydantic).\n- [X] I already checked if it is not related to SQLModel but to [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy).\n\n### Commit to Help\n\n- [X] I commit to help with one of those options 👆\n\n### Example Code\n\n```python\nclass TripInput(SQLModel):\r\n start: int\r\n end: int\r\n description: str\r\n\r\nclass TripOutput(TripInput):\r\n id: int\r\n\r\nclass Trip(TripInput, table=True):\r\n id: Optional[int] = Field(default=None, primary_key = True)\r\n car_id: int = Field(foreign_key=\"car.id\")\r\n car: \"Car\" = Relationship(back_populates=\"trips\")\r\n\r\nclass CarInput(SQLModel):\r\n size: str\r\n fuel: str = \"electric\"\r\n doors: int\r\n transmission: str = \"auto\"\r\n\r\nclass Car(CarInput, table=True):\r\n id: Optional[int] = Field(primary_key = True, default=None)\r\n trips: list[Trip] = Relationship(back_populates=\"car\")\r\n\r\n\r\nclass CarOutput(CarInput):\r\n id: int\r\n trips: list[TripOutput] = []\r\n\r\n@app.get('/api/cars/{id}', response_model=CarOutput)\r\ndef car_by_id(id: int, session: Session = Depends(get_session)) -> Car:\r\n car = session.get(Car, id)\r\n if car:\r\n query = select(Trip).where(Trip.car_id == id)\r\n triplist = session.exec(query).all()\r\n car.trips.append(triplist)\r\n return car\r\n else:\r\n raise HTTPException(status_code=400, detail=f\"No car with id {id} found\")\r\n\r\nstart\tend\tdescription\tid\tcar_id\r\n0\t100\tmanali trip\t1\t1\r\n300\t600\tLeh\t2\t1\r\n0\t20\tMall Visit\t3\t2\r\n\r\nsize\tfuel\tdoors\ttransmission\tid\r\nm\thybrid\t4\tauto\t1\r\nl\thybrid\t5\tauto\t2\n```\n\n\n### Description\n\nI've created a carsharing app, which has car and trip classes. Both have a back populates relationship on them with the other object. I am using SQLite, and database already has a Car table with 2 entries, and trip table with 3 entries as shown in the example code. \r\nI want to return car when a GET call is made with Car_id as input.\r\nBut getting an internal server error as below\r\n car.trips.append(triplist)\r\nAttributeError: 'Car' object has no attribute 'trips'\r\n\r\nread the documentation and still not able to resolve. Appreciate any help.\r\n[code.zip](https://github.com/tiangolo/sqlmodel/files/9162474/code.zip)\r\n \n\n### Operating System\n\nWindows\n\n### Operating System Details\n\n_No response_\n\n### SQLModel Version\n\n0.0.6\n\n### Python Version\n\n3.9.4\n\n### Additional Context\n\n_No response_","author":{"url":"https://github.com/syncopatedNote","@type":"Person","name":"syncopatedNote"},"datePublished":"2022-07-21T20:10:49Z","url":"https://github.com/fastapi/sqlmodel/issues/383"}
| 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:78f36b24-4838-bfc1-e9ab-701ce9392168 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCFE:32F0C3:1A8202:23FD8E:6AFE173C |
| html-safe-nonce | 69d2ee3998a46836aba6ced281fc920d7b59b818d367e60d236153840dc4b73d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0ZFOjMyRjBDMzoxQTgyMDI6MjNGRDhFOjZBRkUxNzNDIiwidmlzaXRvcl9pZCI6IjM1OTIwNzcwOTIxNDI4NDc4MDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d17d1b573d6173f88ad51275220f980e3b6c5fe1a4024f11258842676768e5fb |
| hovercard-subject-tag | issue:1313748727 |
| 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/383/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2f0fcdcedf4890e5bc9b757432e576931a51576ffd18273daddc3c736be9b5c7/fastapi/sqlmodel/issues/383 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2f0fcdcedf4890e5bc9b757432e576931a51576ffd18273daddc3c736be9b5c7/fastapi/sqlmodel/issues/383 |
| 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 | syncopatedNote |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3cd8a1d79ccbf9aecac62781f79072482a49047fb982b8c9e2ad4aca8bf6a9af |
| 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 | 05539640a34afe6142a519483d78ddd766a1304a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width