Title: Calculated columns not working (`sqlaquemy.orm.column_property`) · Issue #240 · fastapi/sqlmodel · GitHub
Open Graph Title: Calculated columns not working (`sqlaquemy.orm.column_property`) · Issue #240 · fastapi/sqlmodel
X Title: Calculated columns not working (`sqlaquemy.orm.column_property`) · Issue #240 · 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/240
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Calculated columns not working (`sqlaquemy.orm.column_property`)","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\r\n\r\nfrom sqlalchemy import func\r\nfrom sqlalchemy import select as sa_select\r\nfrom sqlalchemy.orm import column_property\r\nfrom sqlmodel import create_engine, Field, Relationship, SQLModel\r\n\r\n\r\nclass Hero(SQLModel, table=True):\r\n name: str = Field(primary_key=True)\r\n team_name: str = Field(foreign_key=\"team.name\")\r\n team: \"Team\" = Relationship(back_populates=\"heroes\")\r\n\r\n\r\nclass Team(SQLModel, table=True):\r\n name: str = Field(primary_key=True)\r\n heroes_count = column_property(\r\n sa_select(func.count(Hero.team_name))\r\n .where(Hero.team_name == name)\r\n .correlate_except(Hero)\r\n .scalar_subquery()\r\n )\r\n heroes: List[\"Hero\"] = Relationship(back_populates=\"team\")\r\n\r\n\r\nif __name__ == \"__main__\":\r\n engine = create_engine(\"sqlite:///test.db\")\r\n SQLModel.metadata.create_all(engine)\r\n```\r\n\r\n\r\n### Description\r\n\r\n* Create a Hero model\r\n* Crate a Team model\r\n* Create a calculated column using `sqlalquemy.orm.column_property` (https://docs.sqlalchemy.org/en/14/orm/mapped_sql_expr.html#using-column-property)\r\n* Create all tables and it will raise the `sqlalquemy.exc.InvalidRequestError`\r\n```shell\r\nsqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class Hero-\u003ehero, expression 'Team' failed to locate a name ('Team').\r\nIf this is a class name, consider adding this relationship() to the \u003cclass '__main__.Hero'\u003e class after both dependent classes have been defined.\r\n```\r\n\r\n### Operating System\r\n\r\nmacOS\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.6\r\n\r\n### Additional Context\r\n\r\n_No response_","author":{"url":"https://github.com/angel-langdon","@type":"Person","name":"angel-langdon"},"datePublished":"2022-02-09T12:10:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/240/sqlmodel/issues/240"}
| 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:8f8a4593-72c7-dd62-8cab-94cf1c11998c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8F0:1E946E:1B84843:2578B3F:6A62F495 |
| html-safe-nonce | 22a242db28e4df640a69ad1099e9f63d04f320efe00d09d6c01dfcd7ded80586 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEYwOjFFOTQ2RToxQjg0ODQzOjI1NzhCM0Y6NkE2MkY0OTUiLCJ2aXNpdG9yX2lkIjoiMzI2NDAxODA5NTg1NDY4NTY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 53decc99395e521811c77704e473b32ac152c0f13ac991a4268359e379d9ac3f |
| hovercard-subject-tag | issue:1128459641 |
| 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/240/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dfbaf8aad2cc902ddea51d9fdbc196d90d962839c123eeba5aff001f29033854/fastapi/sqlmodel/issues/240 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dfbaf8aad2cc902ddea51d9fdbc196d90d962839c123eeba5aff001f29033854/fastapi/sqlmodel/issues/240 |
| 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 | angel-langdon |
| hostname | github.com |
| expected-hostname | github.com |
| None | b415018e190e73858133ddcaa36acce7b3f3572fe54dda84bd3b21a6ec714c30 |
| 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 | 22f98521e99f504294ab0812b66104d50eb75a70 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width