Sequelize/Postgres - Finding a chat with ONLY these two users

时间:2018-03-25 21:14:18

标签: node.js postgresql sequelize.js

I have these two models: User and Conversation

User = sequelize.define('user', {
    name: Sequelize.STRING
});

Conversation = sequelize.define('conversation', {
    name: Sequelize.STRING
});

User.belongsToMany(Conversation, { through: 'user_conversation' });
Conversation.belongsToMany(User, { through: 'user_conversation' });

User and Conversation have a many to many relationship.

What I am trying to do is get all conversations that have userA and userB, and NO one else.

I have tried these two methods:

Conversation.findAll({
    include: [{
        model: User,
        where: { id: { Op.and: [ userA.id, userB.id ] }
   }]
});

This doesn't return anything, which makes sense. Id can't be A and B at the same time.

Conversation.findAll({
    include: [{
        model: User,
        where: { id: { Op.or: [ userA.id, userB.id ] }
   }]
});

This returns any conversation that users A and B are participating in.

How can I find any conversation where ONLY users A and B are involved? Can I somehow count the includes?

0 个答案:

没有答案