【问题标题】:How can I load more than 2 sub-relationships at once in SQLAlchemy?如何在 SQLAlchemy 中一次加载超过 2 个子关系?
【发布时间】:2023-03-29 12:25:01
【问题描述】:

我正在尝试将关系向下加载 2 个级别,似乎 joinedload() 函数将我一次限制为 2 个,在 1 个直接关系下。基本上,我正在尝试获得promotion's sponsortypecategory

这是错误:

TypeError: joinedload() takes at most 3 arguments (4 given)

代码:

matrix_engagements = matrix_session.query(MatrixEngagement) \
    .filter(MatrixEngagement.id > latest_goldstrike_engagement_id) \
    .options(joinedload('promotion').joinedload('sponsor', 'type',
                                                'category')) \
    .options(joinedload('player'))\
    .options(joinedload('user_coupon').joinedload('coupon').joinedload(
        'reward'))\
    .options(joinedload('kiosk'))\
    .order_by(MatrixEngagement.id.asc()).limit(10).all()

joinedload()函数:

def joinedload(*keys, **kw):
    return _UnboundLoad._from_keys(
        _UnboundLoad.joinedload, keys, False, kw)

看来应该是无限量keys

【问题讨论】:

    标签: python orm sqlalchemy relationship


    【解决方案1】:

    只需添加具有相同父关系的第二行就可以了:

    ...
    .options(joinedload('promotion').joinedload('sponsor', 'type'))\
    .options(joinedload('promotion').joinedload('category'))\
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 2016-04-25
      • 2014-05-19
      • 2021-02-05
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      相关资源
      最近更新 更多