【问题标题】:Sequelize where condition for nested modelsSequelize 嵌套模型的 where 条件
【发布时间】:2021-06-05 23:14:50
【问题描述】:

我有一个包含注释和用户的数据库。现在我想获取注释内容 = 某物或用户名 = 某物的所有注释。我可以使用 SQL 轻松编写此查询,但无法使用 sequlizer。

Note.findAll({
   include: [
             { model: Users, where: { name: 'something' }}
    ]
})

预期的查询是

select * from users,notes where notes.content='something' or users.name='something'

问题是注释不在包含中,所以我不能使用

where: {
     '$or':{

我的问题是如何使用 sequelize 在嵌套表上设置 where 或条件

【问题讨论】:

    标签: javascript node.js database postgresql sequelize.js


    【解决方案1】:
    Note.findAll({
        where: {
            [Op.or]: [
                { content: { [Op.like]: 'something' } },
                { '$users.name$': { [Op.like]: 'john' } },
            ]
        },
        include: [
            { model: Users, as: 'users', required: true }
        ]
    })
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      相关资源
      最近更新 更多