没有 joinload 的 SQLAlchemy[异步] 查询

问题描述 投票:0回答:1
class Items(Base):
    tag_id = Column(Integer, ForeignKey(Tag.id, ondelete='CASCADE'), index=True)
    ...
    tag = relationship('Tag', back_populates='items')

query = Select(Items)
cursor = await db_session.execute(query)
items = cursor.scalars().all()
print(items[0].tag.id)  # 123

为什么没有

joinedload
它也能工作?第二个表的额外查询什么时候发生?

python sqlalchemy fastapi
1个回答
0
投票

可能是因为这个Tag对象已经在会话缓存中了

© www.soinside.com 2019 - 2024. All rights reserved.