Sequelize error when using “where” and “in” on a subarray

前端 未结 5 1570
一整个雨季
一整个雨季 2021-02-02 06:22

This is my model definition:

var Tag = sequelize.define(\'Tag\', {
    name: Sequelize.STRING
});

var Event = sequelize.define(\'Event\', {
    name: Sequelize.         


        
相关标签:
5条回答
  • 2021-02-02 06:24

    replace in with $in

    where : { id : { $in : [1,2,3,4]}}
    
    0 讨论(0)
  • 2021-02-02 06:45

    in property able to use at version 4.42

    Tag.findAll({
        where: { id: {in: [1,2,3,4]} }
    }).then(...)
    

    And you can use notIn property for excluded values.

    Tag.findAll({
        where: { id: {notIn: [1,2,3,4]} }
    }).then(...)
    

    http://docs.sequelizejs.com/manual/querying.html#operators

    0 讨论(0)
  • 2021-02-02 06:46

    I know this is hella old. But I'm pretty sure what you were looking for is as follows. At least one of these should do the trick. Probably both. I can't test at the moment because I'm in the middle of a 43-files-changed spaghetti code beast. I'll update with an actual test when I find the time.

    (I'm posting now because all the rest seem to not actually answer the question asked, but they did answer the question I had when I arrived here.)

    Event
    .findAndCountAll({
        include: [{model: Tag, as: 'tags'}],
        where: {
          tags: {
            id: {in: [1,2,3,4]}
          }
        },
        order: order,
        limit: pageSize,
        offset: pageSize * (page - 1),
    })
    .success(function(result) {
    
        ...
    });
    
    Event
    .findAndCountAll({
        include: [
          {
            model: Tag,
            as: 'tags',
            where: { id: {in: [1,2,3,4]} },
          }
        ],
        order: order,
        limit: pageSize,
        offset: pageSize * (page - 1),
    })
    .success(function(result) {
    
        ...
    });
    
    0 讨论(0)
  • 2021-02-02 06:47

    No need "in" property, sequelize auto define this. Just set array.

    Tag.findAll({
        where: {
            id: [1,2,3,4]
        }
    }).then(...)
    
    0 讨论(0)
  • 2021-02-02 06:49

    Updated example for Sequelize v5:

    await Tag.findAll({
      where: {
        id: {
          [Sequelize.Op.in]: [1, 2, 3, 4]
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题