我的用户模型中有关系
photos = relationship('UserPhoto', backref='user', lazy='joined')
用
获取所有条目后users = query.all()
SA使用LEFT OUTER JOIN对userPhoto表进行查询
我开始迭代它们并打印所有属性 突然,SA为每个条目提出了奇怪的隐式查询
INFO:sqlalchemy.engine.base.Engine:BEGIN (implicit)
INFO:sqlalchemy.engine.base.Engine:SELECT users.id AS users_id, ... FROM users
WHERE users.id = %(param_1)s
INFO:sqlalchemy.engine.base.Engine:{'param_1': <integer idetifier>}
INFO:sqlalchemy.engine.base.Engine:SELECT user_photo.id... FROM user_photo
WHERE %(param_1)s = user_photo.user_id
INFO:sqlalchemy.engine.base.Engine:{'param_1': <integer idetifier>}
我花了几个小时才找出原因,但仍在搜索
如果我自己构建联接,如下所示:
query.options(joinedload_all('some field'))
SA只使用连接
进行一次选择