I have a hybrid property like this in MyModel:
@hybrid_property
def state(self):
states = [dummy.state_id for dummy in self.dummies.all()]
if all(state == "DUMMY" for state in states):
return State.query.get("DUMMY").text
if all((state == "FAKE" or state == "DUMMY") for state in states):
return State.query.get("FAKE").text
return State.query.get("INVALID").text
And I want to query it in my resource like this:
valid_text = State.query.get("FAKE").text
return data_layer.model.query.filter_by(state=valid_text) # Where data_layer.model is MyModel
But I get an empty array. Doing simply just data_layer.model.query.all() gets me the data so the logic works.
I understand I may need to create an expression for my property instead, but every example I've found are for much simpler use cases.
I tried with this:
@state.expression
def state(cls):
states = [dummy.state_id for dummy in self.dummies.all()]
all_dummies = all(state == "DUMMY" for state in states)
all_fakes_or_dummies = all(
(state == "FAKE" or state == "DUMMY") for state in states
)
dummy_text = State.query.get("DUMMY").text
fake_text = State.query.get("FAKE").text
invalid_text = State.query.get("INVALID").text
return case(
[
(
all_dummies,
dummy_text,
),
(
all_fakes_or_dummies,
fake_text,
),
],
else_=invalid_text,
)
But my resource now returns sqlalchemy.exc.ArgumentError: Ambiguous literal: False. Use the 'text()' function to indicate a SQL expression literal, or 'literal()' to indicate a bound value.
I wonder how could I correctly implement this python logic to be compatible for SQLAlchemy, I guess that must be the problem. Also I wonder whether making such complex logic at a hybrid property is a good practice at all.