sequelize.js - 如何从列中获取行,不包括某些行,这些行的 Id 保存在另一个表中

标签 sequelize.js

'blocked' 表有两列,'userId' 和 'messageId',它是表 'messages' 的外键。

我想在过滤被阻止的消息后获取消息。我试过这个:

messages.findAll({
    where: {
        userId,
    },
    include:[{
        model: 'blocked',
        required: false
        attributes:[],
        where: {
            userId,
        },
    }],
});

没用。有人可以提出解决方案吗?

最佳答案

我会将其分为两个查询

const blocked = await blocked.findAll({
    where: {
        userId,
        attributes: ['messageId']
    }
});

const blockedIds = blocked.map(b => b.messageId);

const messages = await messages.findAll({
    where: {
        id: { $notIn: blockedIds }
    }
});

关于sequelize.js - 如何从列中获取行,不包括某些行,这些行的 Id 保存在另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58040901/

相关文章:

javascript - GraphQL 不允许查询不同字段

node.js - NodeJS & Sequelize : showing all the conversation the current user has

node.js - 由于变量名与列名不同,在我的创建 Controller 中获取 sequelize 属性 notNull 违规

mysql - Sequelize : How to map a custom attribute in a pivot table

node.js - Sequelize - 使用 or 运算符和模型关联查询

mysql - Like Statement 在 sequelize 中对我不起作用

json - ORM : Sequelize: How can I return except some json data?

node.js - 如何使用 sequelize 搜索经纬度的数据库表

javascript - 将 sql 查询转换为 sequelize

mysql - 使用 Sequelize 添加外键约束