Title: There seems to be a bug in using model with aliased fields to define request body · Issue #374 · fastapi/sqlmodel · GitHub
Open Graph Title: There seems to be a bug in using model with aliased fields to define request body · Issue #374 · fastapi/sqlmodel
X Title: There seems to be a bug in using model with aliased fields to define request body · Issue #374 · 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/374
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"There seems to be a bug in using model with aliased fields to define request body ","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 Optional\r\n\r\nimport uvicorn\r\nfrom fastapi import FastAPI\r\nfrom pydantic import BaseModel\r\nfrom sqlmodel import Field, Session, SQLModel, create_engine, select\r\n\r\n\r\nclass Hero(SQLModel, table=True):\r\n id: Optional[int] = Field(default=None, primary_key=True)\r\n name: str = Field(index=True)\r\n secret_name: str = Field(alias='secretName')\r\n age: Optional[int] = Field(default=None, index=True)\r\n\r\n\r\nclass HeroPydantic(BaseModel):\r\n id: Optional[int] = Field(default=None)\r\n name: str = Field()\r\n secret_name: str = Field(alias='secretName')\r\n age: Optional[int] = Field(default=None)\r\n\r\n\r\nsqlite_file_name = \"database.db\"\r\nsqlite_url = f\"sqlite:///{sqlite_file_name}\"\r\n\r\n\r\nconnect_args = {\"check_same_thread\": False}\r\nengine = create_engine(sqlite_url, echo=True, connect_args=connect_args)\r\n\r\n\r\napp = FastAPI()\r\n\r\n\r\n@app.post(\"/heroes/\")\r\ndef create_hero(hero: Hero):\r\n with Session(engine) as session:\r\n session.add(hero)\r\n session.commit()\r\n session.refresh(hero)\r\n return hero\r\n\r\n\r\nif __name__ == '__main__':\r\n uvicorn.run(app, host='0.0.0.0', port=8081)\r\n```\r\n\r\n\r\n### Description\r\n\r\n1. Create `Hero` model, the `secret_name` field aliased to `secretName`\r\n2. Post data\r\n```json\r\n{\r\n \"name\": \"string\",\r\n \"secretName\": \"string\",\r\n \"age\": 0\r\n}\r\n```\r\n3. Then get `hero.secret_name` is `None`\r\n4. If I replace `hero: Hero` to `hero: HeroPydantic`, the `hero.secret_name` can get correct value from post data. But the strainge thing is that I can get correct `hero.secret_name` by `hero: Hero = Hero.parse_obj(hero.dict(by_alias=True))`\r\n\r\n**So it seems that alias argument in sqlmodel seems not working for converting body from post data**, I have to write two models in order to achieve that.\r\n\r\n### Operating System\r\n\r\nWindows\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.7.6\r\n\r\n### Additional Context\r\n\r\n_No response_","author":{"url":"https://github.com/boh5","@type":"Person","name":"boh5"},"datePublished":"2022-07-12T15:31:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/374/sqlmodel/issues/374"}
| 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:5d24779b-5795-7267-09ef-9c19cf38b9f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9058:15403D:28BE8A:39AB97:6A63767A |
| html-safe-nonce | 97af4aae8b7213b28410e847ed5f57a4902478d1c0a4289242662ff648294bcd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDU4OjE1NDAzRDoyOEJFOEE6MzlBQjk3OjZBNjM3NjdBIiwidmlzaXRvcl9pZCI6IjEzNzM0ODk5MjU1Mzk5ODUwMTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f36869b408dd2ae2673ad80d2a4d04dee56afe0dd36f6c5ea5a098d1aef43df2 |
| hovercard-subject-tag | issue:1302227975 |
| 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/374/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6da40501f9e2339a8ab3ad1f8610841aef460d08f179ae77229e62ebd525bfaa/fastapi/sqlmodel/issues/374 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6da40501f9e2339a8ab3ad1f8610841aef460d08f179ae77229e62ebd525bfaa/fastapi/sqlmodel/issues/374 |
| 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 | boh5 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 15e78334bb345f3864e70c5a376ed0383374123629b9e3022398d9431f6cd8bf |
| 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 | 51da2d8d0a841a1393b806a6559fa9ad757e1666 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width