SQLAlchemy ORM搜索属性的多个关系

时间:2018-06-17 02:59:56

标签: python sqlalchemy

我知道可以通过关系属性过滤初始查询(请参阅SqlAlchemy - Filtering by Relationship Attribute

但是,在您已经检索到对象后呢?

假设:

Uri gmmIntentUri = Uri.parse("geo:0,0?q=Big Hoffa's Smokehouse, East Main Street, Westfield, IN");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
    startActivity(mapIntent);
}

我知道我能做到

public interface PokemonService {
@GET("app.php?acao=perfil")
public Call<List<Perfil>> busca(@Query("id") int id);

}

获取所有已关闭公园的清单......

但是,如果我已经检索到某个特定的公园,那么我想查看它是否关闭了?或者查看特定条件是否存在多个警报(关系)。

class Parks(Base):
    __tablename__ = 'parks'

    id = Column(Integer, primary_key=True)
    description = Column(String, nullable=False, unique=True, index=True)
    state = Column(String(2))
    city = Column(String)
    zipcode = Column(String, index=True)

    alerts = relationship('Alerts', back_populates='park')


class Alerts(Base):
    __tablename__ = 'alerts'

    id = Column(Integer, primary_key=True)
    park_id = Column(Integer, ForeignKey('parks.id'))
    alert_type_id = Column(Integer, ForeignKey('alert_types.id'))
    alert_message = Column(String)

    park = relationship('Parks', back_populates="alerts")
    alert_type = relationship('AlertTypes', back_populates='alerts')


class AlertTypes(Base):
    __tablename__ = 'alert_types'

    id = Column(Integer, primary_key=True)
    alert_type_long_des = Column(String)

    alerts = relationship('Alerts', back_populates='alert_type')

park_closed_alert = session.query(AlertTypes)\ .filter(AlertTypes.alert_type_long_des == 'Park Closed').first() closed_parks = session.query(Parks)\ .filter(Parks.alerts.any(alert_type = park_closed_alert )).all() 会给我一个警报列表,我可以轻松地遍历它们......但是因为调用park_record_from_db = session.query(Parks).filter(Parks.longdes == 'City Park').first() 关系会再次查询数据库,我可以直接过滤?

1 个答案:

答案 0 :(得分:0)

可以使用dynamic relationship loaders完成此操作。定义关系时指定lazy=dynamic。然后,该属性将表现为Query对象,可以对其进行过滤而不是立即加载。

alerts = relationship('Alerts', back_populates='park', lazy='dynamic')
[...]
filtered_alerts = park_record_from_db.alerts.filter( \
    Alerts.alert_type.in_((park_closed_alert, park_dogs_alert))).all()

另一个选项是query-enabled properties,它允许更多自定义。

当然,如果要在检索初始对象的位置按关系进行过滤,可以使用any运算符:

park_record_from_db = session.query(Parks) \
    .filter(Parks.longdes == 'City Park') \
    .filter(Parks.alerts.any( \
        Alerts.alert_type.in_((park_closed_alert, park_dogs_alert)))) \
    .first()