Title: Saving Link Model with Extra fields fails · Issue #339 · fastapi/sqlmodel · GitHub
Open Graph Title: Saving Link Model with Extra fields fails · Issue #339 · fastapi/sqlmodel
X Title: Saving Link Model with Extra fields fails · Issue #339 · 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/339
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Saving Link Model with Extra fields fails","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\n# Models\r\n\r\nclass AssemblyEPDLink(SQLModel, table=True):\r\n assembly_id: Optional[str] = Field(default=None, foreign_key=\"assembly.id\", primary_key=True)\r\n epd_id: Optional[str] = Field(default=None, foreign_key=\"epd.id\", primary_key=True)\r\n conversion_factor: float = 1.0\r\n\r\n assembly: \"Assembly\" = Relationship(back_populates=\"layer_links\")\r\n epd: \"EPD\" = Relationship(back_populates=\"assembly_links\")\r\n\r\n\r\nclass Assembly(SQLModel, table=True):\r\n \"\"\"Assembly database class\"\"\"\r\n\r\n id: Optional[str] = Field(default_factory=string_uuid, primary_key=True)\r\n name: str = Field(index=True)\r\n category: str\r\n life_time: float = 50\r\n meta_fields: dict = Field(default=dict, sa_column=Column(JSON), nullable=False)\r\n unit: str = \"M2\"\r\n conversion_factor: float = 1.0\r\n\r\n layer_links: List[AssemblyEPDLink] = Relationship(back_populates=\"assembly\")\r\n\r\n\r\nclass EPD(SQLModel, table=True):\r\n \"\"\"EPD database class\"\"\"\r\n\r\n id: Optional[str] = Field(default_factory=string_uuid, primary_key=True)\r\n name: str = Field(index=True)\r\n category: str\r\n gwp_by_phases: dict = Field(default=dict, sa_column=Column(JSON), nullable=False)\r\n version: str\r\n expiration_date: date\r\n date_updated: date\r\n source: str\r\n\r\n assembly_links: List[AssemblyEPDLink] = Relationship(back_populates=\"epd\")\r\n\r\n# Strawberry Mutation\r\n@strawberry.type\r\nclass Mutation:\r\n @strawberry.mutation(permission_classes=[IsAuthenticated])\r\n async def add_assembly(\r\n self,\r\n info: Info,\r\n name: str,\r\n category: str,\r\n life_time: float | None = 50,\r\n meta_fields: Optional[JSON] = None,\r\n layers: Optional[JSON] = None,\r\n conversion_factor: float | None = 1,\r\n ) -> GraphQLAssembly:\r\n\r\n if meta_fields is None:\r\n meta_fields = {}\r\n\r\n session = info.context.get(\"session\")\r\n\r\n assembly = Assembly(\r\n name=name,\r\n category=category,\r\n life_time=life_time,\r\n conversion_factor=conversion_factor,\r\n meta_fields=meta_fields,\r\n )\r\n\r\n if layers:\r\n query = select(EPD).where(col(EPD.id).in_([layer.get(\"id\") for layer in layers]))\r\n epds = await session.exec(query)\r\n epds = epds.all()\r\n\r\n for epd in epds:\r\n link = AssemblyEPDLink(assembly=assembly, epd=epd, conversion_factor=2)\r\n session.add(link)\r\n else:\r\n session.add(assembly)\r\n \r\n await session.commit()\r\n await session.refresh(assembly)\r\n\r\n return assembly\r\n```\r\n\r\n\r\n### Description\r\n\r\n* I start my test\r\n* Set up a blank database and run `SQLModel.metadata.create_all(bind=engine)`\r\n* I create three EPDs and save them to the database\r\n* Then I post a new mutation to create a new Assembly with those EPDs in \"layers\"\r\n* When I try to commit the AssemblyEPDLink to the session the database throws an error:\r\n * `'asyncpg.exceptions.NotNullViolationError': null value in column \"assembly_id\" of relation \"assemblyepdlink\" violates not-null constraint`\r\n\r\n### Operating System\r\n\r\nLinux\r\n\r\n### Operating System Details\r\n\r\nUbuntu 22.04\r\n\r\n### SQLModel Version\r\n\r\n0.0.6\r\n\r\n### Python Version\r\n\r\n3.10\r\n\r\n### Additional Context\r\n\r\nI have tried to follow the steps outlined in the [docs](https://sqlmodel.tiangolo.com/tutorial/many-to-many/link-with-extra-fields/), adapted to my situation, but somehow the link model doesn't want to save to the database.\r\n\r\nWhen I create an Assembly without a link model everything works fine.\r\n\r\nI have a setup with FastAPI, Strawberry, SQLModel and postgres.","author":{"url":"https://github.com/ocni-dtu","@type":"Person","name":"ocni-dtu"},"datePublished":"2022-05-12T13:18:51Z","url":"https://github.com/fastapi/sqlmodel/issues/339"}
| 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:657965b3-9b5f-3360-6a3e-d86c5e3533c8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 97C2:306EC8:39F26B:524D75:6AFE04D0 |
| html-safe-nonce | 82bb4d1731a468e2023d4994c6de2ebc99784032c617a99e162bb9f849737fb2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0MyOjMwNkVDODozOUYyNkI6NTI0RDc1OjZBRkUwNEQwIiwidmlzaXRvcl9pZCI6IjM4MzI2NDgxMDA2NzY5MDcwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 514201872b83e1d83e530235ce77844838c3470031ef20fce00615264f64a22c |
| hovercard-subject-tag | issue:1233973980 |
| 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/339/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b14af66530706dce5e50e992744f2de275f0c001c8724c8a2af62977d655d934/fastapi/sqlmodel/issues/339 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b14af66530706dce5e50e992744f2de275f0c001c8724c8a2af62977d655d934/fastapi/sqlmodel/issues/339 |
| 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 | ocni-dtu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 86d46ece7c3d9b0562692f81a135cb55ad6a1c82c93ab1ac874d2f1b46b28958 |
| 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 | 5c18e6799976b7bc2f21178024289bb98edbcc85 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width