I have model defined as follow:
class Patch(Base):
id = Column(Integer, primary_key=True)
major = Column(Integer, nullable=False)
minor = Column(Integer, nullable=False)
@hybrid_property
def patch(self) -> str:
return f'{self.major}.{self.minor}'
@patch.expression
def patch(self):
return func.concat(self.major, '.', self.minor)
I would like to make such type of requests:
Patch.query.order_by(Patch.patch)
Which should be equalent to next SQL commands:
SELECT * FROM patch ORDER BY major DESC, minor DESC
I tried to use hybrid_property.expression
@patch.expression
def patch(self):
return self.major, self.minor
but got the exception:
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got '(<sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f1a5cd5fc50>, <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f1a5cd5fd00>)
I know its possible to use custrom comparator, but I havent found how to do that with two fields (i.e. major
, minor
).
Any ideas?
P.S. it doesn't have to be hybrid_property
, either way is fine