【问题标题】:How I can include array of model?如何包含模型数组?
【发布时间】:2020-03-21 22:27:27
【问题描述】:
var user = await db.user.findOne({
      attributes: attributes,
      include: ['charges',
        'availability',
        'practice',// 'services',
        'education',// 'user_role',
        'address',
        'user_location',
        // 'isAvailableStatus',
        { model: db.user_role, attributes: role_attributes },
        {
          model: db.user_service, as: 'services',
          //        ThIS IS MY QUESTION
          include : [db.service_group, as : 'group']
          //
        },
      ],
      where: {id: 0}
    });

User -> user_role-> 其中一个 user_role # 这是有效的
用户 -> 用户服务 -> 服务组数组 # 这不起作用// 我的问题

如何包含模型数组?

【问题讨论】:

    标签: javascript mysql node.js sequelize.js


    【解决方案1】:

    您需要按如下方式修改您的代码。如果您正确定义了关联,那么您将获得预期的结果。

    var user = await db.user.findOne({
      attributes: {
        include: [
          'charges',
          'availability',
          'practice', // 'services',
          'education', // 'user_role',
          'address',
          'user_location'
          // 'isAvailableStatus',
        ]
      },
      include: [
        { model: db.user_role, attributes: role_attributes },
        {
          model: db.user_service,
          as: 'services',
          include: [{ model: db.service_group, as: 'group' }]
        }
      ],
      where: { id: 0 }
    });
    

    希望对你有帮助!

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2020-06-04
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    相关资源
    最近更新 更多