-
-
Notifications
You must be signed in to change notification settings - Fork 681
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
Support hybrid_property, column_property, declared_attr #801
base: main
Are you sure you want to change the base?
Conversation
50Bytes-dev
commented
Feb 14, 2024
class Item(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
value: float
hero_id: int = Field(foreign_key="hero.id")
hero: "Hero" = Relationship(back_populates="items")
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
items: List[Item] = Relationship(back_populates="hero")
@declared_attr
def total_items(cls):
return column_property(cls._total_items_expression())
@classmethod
def _total_items_expression(cls):
return (
select(func.coalesce(func.sum(Item.value), 0))
.where(Item.hero_id == cls.id)
.correlate_except(Item)
.label("total_items")
)
@declared_attr
def status(cls):
return column_property(
select(
case(
(cls._total_items_expression() > 0, "active"), else_="inactive"
)
).scalar_subquery()
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just small suggestions from a random outsider :p (some suggestions might be wrong).
All in all, this is clean and adds a lot of functionality, thanks !
You should look into this @tiangolo !
Co-authored-by: Arthur Woimbée <arthur.woimbee@gmail.com>
Co-authored-by: Arthur Woimbée <arthur.woimbee@gmail.com>
Co-authored-by: Arthur Woimbée <arthur.woimbee@gmail.com>
Please merge it asap :3. I need it, lol. |
hey can we get this merged in |
@tiangolo Could you please take a look at this MR. The functionality is very nice. Would be a nice addition. Doesn't seem to clash with overall design |
Following this example I am getting
Even if I do But I think we can get the same result with
|
Any movement to get this functionality in? |
I haven't got this to work with @declared_attr for this #167 , but I am having the same problem as you with pydantic |
A complete stranger here, but as a heads up: pydantic's stance on |