Flask SQLAlchemy 中的 order_by 关系

15 投票
3 回答
26367 浏览
提问于 2025-04-17 21:41

我正在使用一个Flask应用程序,里面有一个叫做LargeGroupAttendance的模型,它引用了另一个叫做Attendee的模型。我想请求所有符合某些条件的LargeGroupAttendance对象,但我想按照Attendee模型中的某一列进行排序——这可能吗?下面是这两个模型:

""" Attendeee Class """
class Attendee(Base):
    __tablename__ = 'attendee'

    id = Column(Integer, primary_key=True)
    first_name = Column(String(200))
    last_name = Column(String(200))
    year = Column(String(200))
    email = Column(String(100), unique=True)
    dorm = Column(String(100))

    def __init__(self, first_name, last_name, year, email, dorm):
        self.first_name = first_name
        self.last_name = last_name
        self.year = year
        self.email = email
        self.dorm = dorm

    def __repr__(self):
        return '<Attendee %r>' % self.first_name

""" Large Group Attendance Class """
class LargeGroupAttendance(Base):
    __tablename__ = 'large_group_attendance'

    id = Column(Integer, primary_key=True)
    first_time = Column(Integer)

    large_group_id = Column(Integer, ForeignKey('large_group.id'))
    large_group = relationship("LargeGroup", backref=backref('large_group_attendance', order_by=id))

    attendee_id = Column(Integer, ForeignKey('attendee.id'))
    attendee = relationship("Attendee", backref=backref('large_group_attendance', order_by=id))

我需要在我的attendee类中添加什么才能实现这个功能吗?还有,我之前尝试过一个查询,但没有任何输出(也没有错误..)。我哪里出错了呢?

    attendance_records = db.session.query(LargeGroupAttendance).filter_by(large_group_id=event_id).order_by(desc(LargeGroupAttendance.attendee.first_name)) 

3 个回答

2

我尝试了所有其他的解决办法,但都没用。只有在列关系中使用order_by才有效,而不是在反向引用中使用(uselist会返回查询,以确认在查询中添加的顺序)。

sections = db.relationship('ConsultantFormSections', backref=backref('report', uselist=False),order_by='ConsultantFormSections.index.desc()', cascade="all, delete", lazy='dynamic', passive_deletes=True)

或者

sections = db.relationship('ConsultantFormSections', backref=backref('report', uselist=False),order_by='consultant_form_sections.columns.index.desc()', cascade="all, delete", lazy='dynamic', passive_deletes=True)
5

我知道这是一篇旧帖子,但我在搜索的时候找到了它,也许对其他人会有帮助。

""" Attendeee Class """
class Attendee(Base):
    __tablename__ = 'attendee'

    id = Column(Integer, primary_key=True)
    first_name = Column(String(200))
    last_name = Column(String(200))
    year = Column(String(200))
    email = Column(String(100), unique=True)
    dorm = Column(String(100))

    ...

""" Large Group Attendance Class """
class LargeGroupAttendance(Base):
    __tablename__ = 'large_group_attendance'

    ...

    attendee_id = Column(Integer, ForeignKey('attendee.id'))
    attendee = relationship(
        "Attendee",
        backref=backref(
            'large_group_attendance',
            order_by=Attendee.__table__.columns.id
        )
    )
19

我觉得你需要在你的查询中加一个连接,像这样:

.join(LargeGroupAttendance.attendee)

这样最终的查询看起来就会是这个样子:

attendance_records = (db.session.query(LargeGroupAttendance).
    filter_by(large_group_id = event_id).
    join(Attendee, LargeGroupAttendance.attendee).
    order_by(desc(Attendee.first_name))
    )

想要更详细的解释,可以看看这个链接:SQLAlchemy: 如何根据关系字段对查询结果进行排序(order_by)?

撰写回答