假设我有 SQL Alchemy ORM 类:
class Session(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_agent = db.Column(db.Text, nullable=False)
class Run(db.Model):
id = db.Column(db.Integer, primary_key=True)
session_id = db.Column(db.Integer, db.ForeignKey('session.id'))
session = db.relationship('Session', backref=db.backref('runs', lazy='dynamic'))
我想查询以下内容:
((session.id, session.user_agent, session.runs.count())
for session in Session.query.order_by(Session.id.desc()))
但是,这显然是 1+n 个查询,这很糟糕。使用 1 个查询的正确方法是什么?在普通 SQL 中,我会使用以下方法来执行此操作:
SELECT session.id, session.user_agent, COUNT(row.id) FROM session
LEFT JOIN rows on session.id = rows.session_id
GROUP BY session.id ORDER BY session.id DESC