Reputation: 1858
I have a SQL Alchemy mapping:
class Employee(Base):
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
@hybrid_property
def me(self):
return 'Yes!' if self.first_name == 'ritratt' else 'Nope...'
Now I simply want to do a query like so:
session.query(Employee.me).all()
But it does not work because SQL Alchemy expects a column or expression instead of a str
. The solution is to use hybrid_expression
but I do not want to use expressions. I want my mapping to only use hybrid property.
How can I write a mapping with a hybrid_property
that returns a str
and work with query()
?
Upvotes: 0
Views: 529
Reputation: 1858
I found a hacky way around it. I use Python to derive the value and then wrap it either in cast
or concat
:
from sqlalchemy import cast, String
...
@hybrid_property
def me(self):
value = 'Yes!' if self.first_name == 'ritratt' else 'Nope...'
return cast(value, String) # or return concat('', value)
This still seems a bit hacky. Wondering if a better way exists
Upvotes: 1