sequelize子查询不生效

0 0 node.js mysql sequelize javascript
╰╮妖言惑众
╰╮妖言惑众

声望值:50 0人

2018-12-26 09:45:02 提问

关注 0关注

收藏 0收藏, 89浏览

    userOrganizationModel.hasMany(organizationModel, {
        foreignKey: 'organization_id'
    });
    // 此include未生效
    const include = [{
        model: organizationModel,
        required: false,
        where: {
            name: {
                $like: 'ext-%'
            }
        }
    }];

    await userOrganizationModel.findAll({
        include: include // 此include未生效
    })
    
    想实现的原始sql:
    // mysql原生语句select * from user_organization where organization_id in(select id from organization where name like 'ext-%')
请先 登录 后评论

1个回答

  • 90后攻城狮78声望 2018-10-15 15:54

    include好像是嵌套预加载样,我猜是这样写,没有实践过,但是好像可以通过app.Sequelize.literal()直接在查询中写sql语句 userOrganizationModel.findAll({ include: [ { model: organizationModel, as: 'Organizations', attributes: ['id'], where: { name: { $like: 'ext-%' }} } ], where: { organization_id: { $in: Organizations.map(o => o.id) } } })
    请先 登录 后评论

注册新账号

悬赏追问
10
  • 10
  • 20
  • 50
  • 100
  • 200
  • 输入数值
发布追问