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.
Nope. @tiangolo that would be really cool if you can take a look into it.
Reacted by tuna_sonmez, rey, Tharinda Seth Wijesekera, MichadeGroot, Stefan Uddenberg, madispp, Cadel, Louis Manhès, Karol Kozicki, Alex Kosh and 8 more
class Model(SQLModel: table=True):
active_until: Optional[datetime.datetime]
class Config:
arbitrary_types_allowed = True
@hybrid_property
def is_active(self) -> bool:
return self.active_until is not None and self.active_until > datetime.datetime.now()
On 0.0.8 I've got an error:
... python3.10/site-packages/sqlmodel/main.py", line 414, in get_sqlachemy_type
raise ValueError(f"The field {field.name} has no matching SQLAlchemy type")
ValueError: The field is_active has no matching SQLAlchemy type
Reacted by jtpavlock, Alex Kosh, Andrii Bovsunovskyi, Bartłomiej Kurzeja, Evan Renz, Evgeniy Lupashin, Elyas Omar and zihao
I also struggled with column_property and hybrid_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_active work as a plain @property?
@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)
another workaround is to mark the hybrid_property and hybrid_method types as ignored - If you subclass the SQLModel class it can work for all your models without needing to change the hybrid property code
frompydanticimportConfigDictfromsqlmodelimportSQLModelasSQLModel_classSQLModel(SQLModel_):
model_config=ConfigDict(ignored_types=(hybrid_method, hybrid_property))
# use hybrid props as normal...
Reacted by PhilipChng, zihao, Everson Magalhães Cavalcante and Noam Levi
Activity
alexbojko commented on Apr 15, 2022
I faced with the same issue
lhs950204 commented on May 14, 2022
me too. solved @alexbojko ...? please...
alexbojko commented on May 14, 2022
Nope. @tiangolo that would be really cool if you can take a look into it.
kozickikarol commented on Sep 17, 2022
Any update regarding this issue?
My model:
class Model(SQLModel: table=True): active_until: Optional[datetime.datetime] class Config: arbitrary_types_allowed = True @hybrid_property def is_active(self) -> bool: return self.active_until is not None and self.active_until > datetime.datetime.now()On 0.0.8 I've got an error:
... python3.10/site-packages/sqlmodel/main.py", line 414, in get_sqlachemy_type raise ValueError(f"The field {field.name} has no matching SQLAlchemy type") ValueError: The field is_active has no matching SQLAlchemy typeAugustinio commented on Sep 19, 2022
Same issue over here @kozickikarol, any workarounds?
antont commented on Oct 20, 2022
I also struggled with column_property and hybrid_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_active work as a plain @property?
Elyasomer commented on Oct 17, 2023
@tiangolo Bro is there any problem in pull request if it be merged it would really help :)
jjjacksn commented on 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 commented on 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 commented on Feb 8, 2024
@jjjacksn Could you please elaborate how to add an expression to use the detection_time in a query?
For instance:
50Bytes-dev commented on Feb 14, 2024
@geudrik use my PR #801
marianafmedeiros commented on Jul 11, 2024
Hi, all! In case this is any help:
The hybrid_property function has a keyword argument expr which 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 commented on Mar 1, 2025
another workaround is to mark the hybrid_property and hybrid_method types as ignored - If you subclass the SQLModel class it can work for all your models without needing to change the hybrid property code