Title: Are Many-to-Many link supported with fastapi? · Issue #121 · fastapi/sqlmodel · GitHub
Open Graph Title: Are Many-to-Many link supported with fastapi? · Issue #121 · fastapi/sqlmodel
X Title: Are Many-to-Many link supported with fastapi? · Issue #121 · 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/121
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Are Many-to-Many link supported with fastapi?","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\nfrom typing import List, Optional\r\n\r\nfrom fastapi import Depends, FastAPI, HTTPException, Query\r\nfrom sqlmodel import Field, Relationship, Session, SQLModel, create_engine\r\n\r\nsqlite_file_name = \"test.db\"\r\nsqlite_url = f\"sqlite:///{sqlite_file_name}\"\r\n\r\n# version sans fastapi: engine = create_engine(sqlite_url, echo=True)\r\nconnect_args = {\"check_same_thread\": False}\r\nengine = create_engine(sqlite_url, echo=True, connect_args=connect_args)\r\n\r\ndef create_db_and_tables():\r\n SQLModel.metadata.create_all(engine)\r\n\r\nclass MPartMagnetLink(SQLModel, table=True):\r\n \"\"\"\r\n MPart/Magnet many to many link table\r\n \"\"\"\r\n magnet_id: Optional[int] = Field(\r\n default=None, foreign_key=\"magnet.id\", primary_key=True\r\n )\r\n mpart_id: Optional[int] = Field(\r\n default=None, foreign_key=\"mpart.id\", primary_key=True\r\n )\r\n\r\n\r\nclass MagnetBase(SQLModel):\r\n \"\"\"\r\n Magnet\r\n \"\"\"\r\n name: str\r\n\r\nclass Magnet(MagnetBase, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n mparts: List[\"MPart\"] = Relationship(back_populates=\"magnets\", link_model=MPartMagnetLink)\r\n\r\nclass MagnetRead(MagnetBase):\r\n id: int\r\n\r\nclass MagnetCreate(MagnetBase):\r\n pass\r\n\r\nclass MagnetUpdate(SQLModel):\r\n \"\"\"\r\n Magnet\r\n \"\"\"\r\n name: str\r\n mparts: List[\"MPart\"] = [] #Relationship(back_populates=\"magnets\", link_model=MPartMagnetLink)\r\n\r\nclass MPartBase(SQLModel):\r\n \"\"\"\r\n Magnet Part\r\n \"\"\"\r\n name: str\r\n\r\nclass MPart(MPartBase, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n magnets: List[Magnet] = Relationship(back_populates=\"mparts\", link_model=MPartMagnetLink)\r\n\r\nclass MPartRead(MPartBase):\r\n id: int\r\n\r\nclass MPartCreate(MPartBase):\r\n pass\r\n\r\nclass MPartUpdate(SQLModel):\r\n \"\"\"\r\n Magnet Part\r\n \"\"\"\r\n name: str\r\n magnets: List[Magnet] = []\r\n\r\nclass MPartReadWithMagnet(MPartRead):\r\n magnets: List[MagnetRead] = []\r\n\r\nclass MagnetReadWithMParts(MagnetRead):\r\n mparts: List[MPartRead] = []\r\n\r\n\r\ndef get_session():\r\n with Session(engine) as session:\r\n yield session\r\n\r\napp = FastAPI()\r\n\r\n@app.patch(\"/magnets/{magnet_id}\", response_model=MagnetRead)\r\ndef update_magnet(\r\n *, session: Session = Depends(get_session), magnet_id: int, magnet: MagnetUpdate):\r\n db_magnet = session.get(Magnet, magnet_id)\r\n if not db_magnet:\r\n raise HTTPException(status_code=404, detail=\"Magnet not found\")\r\n magnet_data = magnet.dict(exclude_unset=True)\r\n for key, value in magnet_data.items():\r\n setattr(db_magnet, key, value)\r\n session.add(db_magnet)\r\n session.commit()\r\n session.refresh(db_magnet)\r\n return db_magnet\r\n\r\n@app.on_event(\"startup\")\r\ndef on_startup():\r\n create_db_and_tables()\n```\n\n\n### Description\n\n* Start uvicorn\r\n* Open browser: it crashes:\r\n```\r\n2021-10-04 15:53:27,962 INFO sqlalchemy.engine.Engine COMMIT\r\nINFO: Application startup complete.\r\nINFO: 127.0.0.1:54084 - \"GET /docs HTTP/1.1\" 200 OK\r\nINFO: 127.0.0.1:54084 - \"GET /openapi.json HTTP/1.1\" 500 Internal Server Error\r\nERROR: Exception in ASGI application\r\nTraceback (most recent call last):\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py\", line 375, in run_asgi\r\n result = await app(self.scope, self.receive, self.send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py\", line 75, in __call__\r\n return await self.app(scope, receive, send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/fastapi/applications.py\", line 208, in __call__\r\n await super().__call__(scope, receive, send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/applications.py\", line 112, in __call__\r\n await self.middleware_stack(scope, receive, send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/middleware/errors.py\", line 181, in __call__\r\n raise exc from None\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/middleware/errors.py\", line 159, in __call__\r\n await self.app(scope, receive, _send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/exceptions.py\", line 82, in __call__\r\n raise exc from None\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/exceptions.py\", line 71, in __call__\r\n await self.app(scope, receive, sender)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/routing.py\", line 580, in __call__\r\n await route.handle(scope, receive, send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/routing.py\", line 241, in handle\r\n await self.app(scope, receive, send)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/starlette/routing.py\", line 52, in app\r\n response = await func(request)\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/fastapi/applications.py\", line 161, in openapi\r\n return JSONResponse(self.openapi())\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/fastapi/applications.py\", line 136, in openapi\r\n self.openapi_schema = get_openapi(\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/fastapi/openapi/utils.py\", line 387, in get_openapi\r\n definitions = get_model_definitions(\r\n File \"/home/vscode/.local/lib/python3.8/site-packages/fastapi/utils.py\", line 24, in get_model_definitions\r\n m_schema, m_definitions, m_nested_models = model_process_schema(\r\n File \"pydantic/schema.py\", line 548, in pydantic.schema.model_process_schema\r\n File \"pydantic/schema.py\", line 589, in pydantic.schema.model_type_schema\r\n File \"pydantic/schema.py\", line 241, in pydantic.schema.field_schema\r\n File \"pydantic/schema.py\", line 440, in pydantic.schema.field_type_schema\r\n File \"pydantic/schema.py\", line 773, in pydantic.schema.field_singleton_schema\r\n File \"pydantic/schema.py\", line 667, in pydantic.schema.field_singleton_sub_fields_schema\r\n File \"pydantic/schema.py\", line 495, in pydantic.schema.field_type_schema\r\n File \"pydantic/schema.py\", line 839, in pydantic.schema.field_singleton_schema\r\n File \"/usr/lib/python3.8/abc.py\", line 102, in __subclasscheck__\r\n return _abc_subclasscheck(cls, subclass)\r\nTypeError: issubclass() arg 1 must be a class\r\n```\r\n\n\n### Operating System\n\nLinux\n\n### Operating System Details\n\nUbuntu 20.04 LTS\n\n### SQLModel Version\n\n0.0.4\n\n### Python Version\n\nPython 3.8.10\n\n### Additional Context\n\nI've tried to adapt the tutorial example with the many-to-many associative tables\r\nfor using it with fastapi without success. If this feature is supported it would be great\r\nto have an example in the tutorial.\r\n\r\nBest\r\n\r\n","author":{"url":"https://github.com/Trophime","@type":"Person","name":"Trophime"},"datePublished":"2021-10-04T15:58:53Z","url":"https://github.com/fastapi/sqlmodel/issues/121"}
| 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:65da26fa-4094-7a6e-ce02-141877cc904c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8FCE:153A9C:3F0E23:5980B5:6AFE0687 |
| html-safe-nonce | a5d4938ed4d508d440d20128e92b9249e4f58defca750ae5ff283cc8bee00e95 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkNFOjE1M0E5QzozRjBFMjM6NTk4MEI1OjZBRkUwNjg3IiwidmlzaXRvcl9pZCI6IjI2Mjg0MDk4MzcxOTExODQ3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d9f071f6f14767213d879363619288f9265a92d398214fd782fbc5f692a6788c |
| hovercard-subject-tag | issue:1015355548 |
| 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/121/issue_layout |
| twitter:image | https://opengraph.githubassets.com/efb692c74c97f49ff4e0602570b0ec649653013a7ea2c1874726a4123a85db54/fastapi/sqlmodel/issues/121 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/efb692c74c97f49ff4e0602570b0ec649653013a7ea2c1874726a4123a85db54/fastapi/sqlmodel/issues/121 |
| 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 | Trophime |
| 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