gpt4 book ai didi

python - SQL炼金术 : eager loading relationships of relationships of relationship

转载 作者:行者123 更新时间:2023-11-28 17:36:27 27 4
gpt4 key购买 nike

我有这样的结构:

class User(DeclarativeBase):
...
teamMemberships = orm.relationship("TeamXREF",backref="user",lazy = "dynamic")


class TeamXREF(DeclarativeBase):
...


class Team(DeclarativeBase):
...
name=db.Column(String)
teamMembers = orm.relationship("TeamXREF",backref="team",lazy = "dynamic")

但是,我无法在 1 个查询中编写(我总是以循环和多个查询结束)急切地一次加载所有这些信息:

  • user.teamMemberships
  • user.teamMemberships.team
  • user.teamMemberships.team.teamMembers
  • user.teamMemberships.team.teamMembers.user

最佳答案

您可能会尝试在查询中显式加载项目,而不是将其硬编码到您的关系中。像这样:

from sqlalchemy.orm import joinedload
Session.query(User).options(joinedload('teamMemberships').joinedload('team').joinedload('teamMembers').joinedload('user'))

http://docs.sqlalchemy.org/en/improve_toc/orm/loading_relationships.html#loading-along-paths

关于python - SQL炼金术 : eager loading relationships of relationships of relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30004293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com