Flask SQLAlchemy order_by relationship Flask SQLAlchemy order_by relationship flask flask

Flask SQLAlchemy order_by relationship


I think you need add a join to your query, something like this:

.join(LargeGroupAttendance.attendee)

so that the final query would look like this:

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

See SQLAlchemy: How to order query results (order_by) on a relationship's field? for a more detailed explanation


I know this is an old post, but it should up when I was searching, so maybe this will be useful to someone else

""" 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        )    )