You can do it, but you need to use an SQL conditional rather than a
python one. In this case that would probably be a CASE expression:

https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.case

I think it would look something like this:

from sqlalchemy.sql import case

aggr = column_property(
    case([(attr_a == 'go', attr_b + attr_c)],
         else_=attr_b - attr_c)
)

Hope that helps,

Simon

On Fri, Dec 11, 2020 at 9:13 AM [email protected] <[email protected]> wrote:
>
> I have a mapped class:
>
> class Model(sqlalchemy.declarative_base()):
>     attr_a = Column(String)
>     attr_b = Column(Integer)
>     attr_c = Column(Integer)
>
>     aggr = column_property(attr_b + attr_c IF attr_a=='go' ELSE attr_b - 
> attr_c)
>
> Last line is pseoudo code that requires some conditional logic. Is such logic 
> allowed inside column_property? Thanks a lot!
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/736f0ded-e39a-47fd-a0db-8ed33057d2a3n%40googlegroups.com.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeO2qgWeKeWPN-WH9LD8_Zch4jQRYeiB-WNbZrcMBrZFQ%40mail.gmail.com.

Reply via email to