我有@hybrid_property
引用嵌套关系self.establishment_type.establishment_base_type.name == 'QSR'
。它适用于assert location.is_qsr == True
中的Location对象,但不适用于过滤器。我尝试添加@is_qsr.expression
函数,但无法使其中任何一个正常工作。如何启用query(Location).filter(Location.is_qsr == True)
?
class Location(Base):
__tablename__ = 'houses'
id = Column(Integer, primary_key=True)
establishment_type_id = Column(
Integer, ForeignKey('establishment_types.id')
)
establishment_type = relationship('EstablishmentType')
@hybrid_property
def is_qsr(self):
if self.establishment_type:
if self.establishment_type.establishment_base_type:
return self.establishment_type.establishment_base_type.name == 'QSR'
return False
class EstablishmentType(Base):
__tablename__ = 'establishment_types'
id = Column(Integer, primary_key=True)
establishment_base_type_id = Column(
Integer, ForeignKey('establishment_base_types.id')
)
establishment_base_type = relationship('EstablishmentBaseType')
class EstablishmentBaseType(Base):
__tablename__ = 'establishment_base_types'
id = Column(Integer, primary_key=True)
答案 0 :(得分:1)
您可以对关系使用.has
:
allows
这不会产生世界上最有效的查询(它会产生@is_qsr.expression
def is_qsr(cls):
return cls.establishment_type.has(
EstablishmentType.establishment_base_type.has(
EstablishmentBaseType.name == "QSR"))
)但是一个体面的优化器应该能够解决它。