Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据关系确定炼金术顺序_Python_Sqlalchemy_Flask_Sql Order By_Flask Sqlalchemy - Fatal编程技术网

Python 根据关系确定炼金术顺序

Python 根据关系确定炼金术顺序,python,sqlalchemy,flask,sql-order-by,flask-sqlalchemy,Python,Sqlalchemy,Flask,Sql Order By,Flask Sqlalchemy,我正在使用一个Flask应用程序,其中我有一个大型的GroupAttention模型,它引用了另一个名为Attendee的模型。我试图请求所有符合特定条件的LargeGroupAttention对象,但我试图按照Attendee模型的列对它们进行排序——这可能吗?以下是两个模型: """ Attendeee Class """ class Attendee(Base): __tablename__ = 'attendee' id = Column(Integer, primar

我正在使用一个Flask应用程序,其中我有一个大型的GroupAttention模型,它引用了另一个名为Attendee的模型。我试图请求所有符合特定条件的LargeGroupAttention对象,但我试图按照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))

我认为您需要在查询中添加一个连接,类似这样:

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

有关更详细的说明,请参见

非常感谢!这正是我需要的!可能重复的
attendance_records = (db.session.query(LargeGroupAttendance).
    filter_by(large_group_id = event_id).
    join(Attendee, LargeGroupAttendance.attendee).
    order_by(desc(Attendee.first_name))
    )