我有一些表想要执行基于 ORM 的查询。
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
is_active = Column(Boolean, server_default="1", nullable=False)
is_deleted = Column(Boolean, server_default="0", nullable=False)
created_at = Column(DateTime, nullable = False, default=func.now())
first_name = Column(String(30), nullable=False)
surname_name = Column(String(30), nullable=False)
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
is_active = Column(Boolean, server_default="1", nullable=False)
is_deleted = Column(Boolean, server_default="0", nullable=False)
created_at = Column(DateTime, nullable = False, default=func.now())
first_name = Column(String(30), nullable=False)
surname_name = Column(String(30), nullable=False)
appointments = relationship("ChildAppointment", backref="child")
class ParentChild(Base):
'''provides a many to many relationship for parent and child'''
__tablename__ = 'parent_child'
id = Column(Integer, primary_key=True)
is_active = Column(Boolean, nullable=False, server_default="1")
is_deleted = Column(Boolean, nullable=False, server_default="0")
parent_id = Column(Integer, ForeignKey('parent.id'), nullable=False)
child_id = Column(Integer, ForeignKey('child.id'))
parents = relationship("Parent", backref="parent_child")
children = relationship("Child", backref="parent_child")
class ChildAppointment(Base):
__tablename__ = 'child_appointment'
id = Column(Integer, primary_key=True)
is_active = Column(Boolean, nullable=False, server_default="1")
is_deleted = Column(Boolean, nullable=False, server_default="0")
created_at = Column(DateTime, nullable = False, default=func.now())
child_id = Column(Integer, ForeignKey('child.id'))
date_appointment = Column(DateTime, nullable = False)
我想查询表 ParentChild
并通过 SQLAlchemy 关系魔法,我想获取 child 的最新约会日期,这意味着我需要按 date_appointment
排序的结果子表。
我按照@zeek here 的例子进行操作我提出了以下建议:
for data in session.query(ParentChild).join(Child.appointments).filter(ParentChild.parent_id == 1).\
order_by(Child.appointments.date_appointment.desc()).all():
但是,我收到错误attributeError:与 Child.appointments 关联的“InstrumentedAttribute”对象和“Comparator”对象均没有属性“date_appointment”
我的场景破坏了该链接中给出的内容,因为我的场景中有一张额外的 table ,因为我有 4 张 table ,所以我无法调整他的答案以适合我的。
谢谢。
最佳答案
有两件事:首先,对于连接条件,您需要将其指定为一种链:
- 父子->子
- child -> child 约会
这样做是这样的:
session.query(ParentChild).join(ParentChild.children).join(Child.appointments)
这将产生正确的连接。我认为如果明确的话你也可以这样做:
session.query(ParentChild).join(Child).join(ChildAppointment)
但是你必须尝试一下。
然而,真正的错误来自这部分:
Child.appointments.date_appointment
它应该看起来像这样:
ChildAppointment.date_appointment
SQLAlchemy 已经知道如何获取与 Child
和 ParentChild
相关的 ChildAppointment
,因为您在 join
中指定了它> 如上所述。如果您用原始 SQL 术语来思考它,那么这种方式就更有意义。
所以你最终的解决方案:
session.query(ParentChild).join(ParentChild.children).join(Child.appointments).filter(ParentChild.parent_id == 1).order_by(ChildAppointment.date_appointment.desc()).all():
关于python - 通过 'relationship' 中定义的链接对结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19604312/