I'm using SQLAlchemy and I have the business rule: "Foo is ready if all their Bar children are ready" and in a use case I need to get all Foo that are ready so I'm trying to do the following query:
Session.query(Foo).filter(Foo.is_ready())
But I'm getting exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/orm/attributes.py", line 185, in __getattr__
return getattr(self.comparator, key)
AttributeError: 'Comparator' object has no attribute 'all'
models
class Foo(Base):
bars = relationship(Bar, lazy = "dynamic")
@classmethod
def is_ready(self):
return len(self.bar.all()) == len(self.bars.filter(Bar.status == "ready"))
class Bar(Base):
status = Column(String)
foo_id = Column(Integer, ForeignKey("foo.id"))
What am I doing wrong? I really need to implement a method Foo.is_ready() since the business rule would be more complex in the future so it is important encapsulate that behavior in order to reutilize later