【发布时间】:2019-08-29 04:48:20
【问题描述】:
我已将我的模型定义为:
class Row(Base):
__tablename__ = "row"
id = Column(Integer, primary_key=True)
key = Column(String(32))
value = Column(String(32))
status = Column(Boolean, default=True)
parent_id = Column(Integer, ForeignKey("table.id"))
class Table(Base):
__tablename__ = "table"
id = Column(Integer, primary_key=True)
name = Column(String(32), nullable=False, unique=True)
rows=relationship("Row", cascade="all, delete-orphan")
要从数据库中读取表,我可以简单地查询表,它会加载表所拥有的所有行。但是,如果我想通过 'status == True' 过滤行,它就不起作用。我知道这不是一个有效的查询,但我想做类似的事情:
session.query(Table).filter(Table.name == name, Table.row.status == True).one()
由于我无法使上述查询工作,我想出了一个新的解决方案,首先查询表而不加载任何行,然后使用 Id 使用过滤器查询 Rows,然后将结果分配给 Table 对象:
table_res = session.query(Table).option(noload('rows')).filter(Table.name == 'test').one()
rows_res = session.query(Row).filter(Row.parent_id == 1, Row.status == True)
table_res.rows = rows_res
但我相信必须有更好的方法一次性做到这一点。有什么建议吗?
【问题讨论】:
标签: python sqlalchemy