Using hybrid_property of SQLAlchemy leads to CompileError #1952
Replies: 13 comments
-
|
I faced with the same issue |
Beta Was this translation helpful? Give feedback.
-
|
me too. solved @alexbojko ...? please... |
Beta Was this translation helpful? Give feedback.
-
|
Nope. @tiangolo that would be really cool if you can take a look into it. |
Beta Was this translation helpful? Give feedback.
-
|
Any update regarding this issue? My model: On 0.0.8 I've got an error: |
Beta Was this translation helpful? Give feedback.
-
|
Same issue over here @kozickikarol, any workarounds? |
Beta Was this translation helpful? Give feedback.
-
|
I also struggled with Am also using normal Python properties where suitable, actually, wouldn't that |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo Bro is there any problem in pull request if it be merged it would really help :) |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
-
|
@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) |
Beta Was this translation helpful? Give feedback.
-
|
@jjjacksn Could you please elaborate how to add an expression to use the For instance: |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
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
) |
Beta Was this translation helpful? Give feedback.
-
|
another workaround is to mark the from pydantic import ConfigDict
from sqlmodel import SQLModel as SQLModel_
class SQLModel(SQLModel_):
model_config = ConfigDict(ignored_types=(hybrid_method, hybrid_property))
# use hybrid props as normal... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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
floatreturn 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
Beta Was this translation helpful? Give feedback.
All reactions