Title: Using hybrid_property of SQLAlchemy leads to CompileError · Issue #299 · fastapi/sqlmodel · GitHub
Open Graph Title: Using hybrid_property of SQLAlchemy leads to CompileError · Issue #299 · fastapi/sqlmodel
X Title: Using hybrid_property of SQLAlchemy leads to CompileError · Issue #299 · 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/299
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using hybrid_property of SQLAlchemy leads to CompileError","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 sqlalchemy.ext.hybrid import hybrid_property \r\nfrom typing import Optional \r\nfrom sqlmodel import SQLModel, Field, create_engine \r\n \r\nfrom datetime import datetime \r\n \r\n \r\nclass ObjectTable(SQLModel, table=True): \r\n object_id: Optional[int] = Field(primary_key=True, default=None) \r\n first_detection_time: datetime = Field(index=True) \r\n last_detection_time: datetime = Field(index=True) \r\n \r\n @hybrid_property \r\n def detection_time(self) -\u003e float: \r\n return (self.last_detection_time - self.first_detection_time).total_seconds() \r\n \r\n class Config: \r\n arbitrary_types_allowed = True \r\n \r\n \r\nif __name__ == \"__main__\": \r\n engine = create_engine(\"sqlite:///database.db\") \r\n SQLModel.metadata.create_all(engine)\n```\n\n\n### Description\n\nI am trying to create a hybrid property in an SQLModel class to allow more complex querying. Following the steps as described in the sqlalchemy docs here: https://docs.sqlalchemy.org/en/14/orm/extensions/hybrid.html I assumed that this would work and create a valid table. However, this code gives the error:\r\n `sqlalchemy.exc.CompileError: (in table 'objecttable', column detection_time'): Can't generate DDL for NullType(); did you forget to specify a type on this Column?`\r\nAt first, I assumed that a type hint was missing so I added the `float` return type to the `hybrid_property`. I am not sure what the problem is exactly but I assumed that this code would yield a valid table.\n\n### Operating System\n\nLinux\n\n### Operating System Details\n\nUbuntu 20.04\n\n### SQLModel Version\n\n0.0.6\n\n### Python Version\n\nPython 3.8.10\n\n### Additional Context\n\n_No response_","author":{"url":"https://github.com/deZakelijke","@type":"Person","name":"deZakelijke"},"datePublished":"2022-04-14T11:27:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":13},"url":"https://github.com/299/sqlmodel/issues/299"}
| 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:8f37112f-34cf-f890-c11f-fc688c8ab0e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DDD4:3EA203:8967BB:C1639F:6A624C54 |
| html-safe-nonce | 9eadab35c0be2ce30f2825bb1138e46e8108e9c3492d861811a3d4426c57cddc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEREQ0OjNFQTIwMzo4OTY3QkI6QzE2MzlGOjZBNjI0QzU0IiwidmlzaXRvcl9pZCI6Ijc5NDQ4ODc2NzE5NDAzMzY3MjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 116d68536983a19de66c90855b874c6ecf01f62c34139bc66470b6a527efa359 |
| hovercard-subject-tag | issue:1204410009 |
| 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/299/issue_layout |
| twitter:image | https://opengraph.githubassets.com/67fff11b8558c7f8c8d0d3c7ca61b814b9f0623007b05aa0371442ddcf411f5b/fastapi/sqlmodel/issues/299 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/67fff11b8558c7f8c8d0d3c7ca61b814b9f0623007b05aa0371442ddcf411f5b/fastapi/sqlmodel/issues/299 |
| 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 | deZakelijke |
| hostname | github.com |
| expected-hostname | github.com |
| None | 77f5af7d0fa3843b1779a53f60ec016b3f962e46051fb05c2e9b608a8138eefb |
| 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 | d3ce7d369aae307e197872f810f80f3ed9051c78 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width