Flask SQLAlchemy order_by relationship

Question:

I’m working with a Flask application where I have a LargeGroupAttendance model that references another model called Attendee. I’m trying to request all of the LargeGroupAttendance objects that match a certain criteria, but I’m trying to sort them by a column of the Attendee model – is that even possible? Here are the two models below:

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

Do I need to add something to my attendee class to make this possible? And here’s a query I’ve tried before, but it’s had no output (no errors either..). Where am I going wrong?

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

||

Answers:

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

Answered By: bwbrowning

I know this is an old post, but it showed 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
        )
    )
Answered By: dvaerum