-
-
Notifications
You must be signed in to change notification settings - Fork 635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using hybrid_property of SQLAlchemy leads to CompileError #299
Comments
I faced with the same issue |
me too. solved @alexbojko ...? please... |
Nope. @tiangolo that would be really cool if you can take a look into it. |
Any update regarding this issue? My model:
On 0.0.8 I've got an error:
|
Same issue over here @kozickikarol, any workarounds? |
I also struggled with Am also using normal Python properties where suitable, actually, wouldn't that |
@tiangolo Bro is there any problem in pull request if it be merged it would really help :) |
I found a somewhat simple workaround: annotate the property as a class var (which pydantic will ignore) from typing import ClassVar, Optional
from datetime import datetime
from sqlalchemy.ext.hybrid import hybrid_property
from sqlmodel import SQLModel, Field, create_engine
def _detection_time(self) -> float:
return (self.last_detection_time - self.first_detection_time).total_seconds()
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)
detection_time: ClassVar[float] = hybrid_property(_detection_time)
if __name__ == "__main__":
engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine) NOTE: I think in this example you need to add an expression for this to work as expected in queries. |
@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) |
@jjjacksn Could you please elaborate how to add an expression to use the For instance:
|
Hi, all! In case this is any help: The Therefore, I believe something like this could work: def _detection_time(self) -> float:
return (self.last_detection_time - self.first_detection_time).total_seconds()
def _detection_time_exp(cls):
return (cls.last_detection_time - cls.first_detection_time)
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)
detection_time: ClassVar[float] = hybrid_property(
_detection_time,
expr=_detection_time_exp
) I've done this myself in a project and it works: def lookup_name_property(self) -> str:
return generate_lookup_name(self.name)
def lookup_name_exp(cls):
"""
Same as `generate_lookup_name(self.legal_name)`, but in SQL
"""
return func.regexp_replace(
func.regexp_replace(
cls.name,
r"\W+",
"_",
"g"
),
r"(^_|_$)",
"",
"g"
)
class MyTable(SQLModel, table=True):
"""table description"""
__table_args__ = {"schema": schema}
__tablename__ = "my_table"
name: str
lookup_name: ClassVar[str] = hybrid_property(
lookup_name_property,
expr=lookup_name_exp
) |
First Check
Commit to Help
Example Code
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 thehybrid_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
The text was updated successfully, but these errors were encountered: