Repository navigation
Using hybrid_property of SQLAlchemy leads to CompileError #299
Copy link
Copy link
Closed
Labels
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 SQLModel" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from sqlalchemy.ext.hybrid import hybrid_property
from typing import Optional
from sqlmodel import SQLModel, Field, create_engine
from datetime import datetime
class ObjectTable(SQLModel, table=True):
object_id: Optional[int] = Field(primary_key=True, default=None)
first_detection_time: datetime = Field(index=True)
last_detection_time: datetime = Field(index=True)
@hybrid_property
def detection_time(self) -> float:
return (self.last_detection_time - self.first_detection_time).total_seconds()
class Config:
arbitrary_types_allowed = True
if __name__ == "__main__":
engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine)Description
I 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:
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?
At 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.
Operating System
Linux
Operating System Details
Ubuntu 20.04
SQLModel Version
0.0.6
Python Version
Python 3.8.10
Additional Context
No response
Reactions are currently unavailable
Activity
alexbojko commentedon Apr 15, 2022
I faced with the same issue
lhs950204 commentedon May 14, 2022
me too. solved @alexbojko ...? please...
alexbojko commentedon May 14, 2022
Nope. @tiangolo that would be really cool if you can take a look into it.
kozickikarol commentedon Sep 17, 2022
Any update regarding this issue?
My model:
On 0.0.8 I've got an error:
Augustinio commentedon Sep 19, 2022
Same issue over here @kozickikarol, any workarounds?
antont commentedon Oct 20, 2022
I also struggled with
column_propertyandhybrid_property. Am now using workaround where just call custom code to add properties in code to my "read" objects.Am also using normal Python properties where suitable, actually, wouldn't that
is_activework as a plain@property?Elyasomer commentedon Oct 17, 2023
@tiangolo Bro is there any problem in pull request if it be merged it would really help :)
jjjacksn commentedon Oct 30, 2023
I found a somewhat simple workaround: annotate the property as a class var (which pydantic will ignore)
NOTE: I think in this example you need to add an expression for this to work as expected in queries.
geudrik commentedon Jan 24, 2024
@tiangolo any update on this yet? The above feels like a work-around (and I'm so far not able to hybrid props and comparators working using sqlmodel and fast api)
christianholland commentedon Feb 8, 2024
@jjjacksn Could you please elaborate how to add an expression to use the
detection_timein a query?For instance:
50Bytes-dev commentedon Feb 14, 2024
@geudrik use my PR #801
marianafmedeiros commentedon Jul 11, 2024
Hi, all! In case this is any help:
The
hybrid_propertyfunction has a keyword argumentexprwhich you can assign the expression function.Therefore, I believe something like this could work:
I've done this myself in a project and it works:
sneakers-the-rat commentedon Mar 1, 2025
another workaround is to mark the
hybrid_propertyandhybrid_methodtypes as ignored - If you subclass the SQLModel class it can work for all your models without needing to change the hybrid property code