83

This is my model definition:

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

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

Event.hasMany(Tag, {as: 'tags', through: 'event_tags', foreignKey: 'eventId'});
Tag.hasMany(Event, {as: 'events', through: 'event_tags', foreignKey: 'tagId'});

In words this is: there are events and tags. Events are tagged with many tags.

I'm trying to run this query:

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) {

    ...
});

But I'm getting this error:

   \node_modules\sequelize\lib\dialects\abstract\query-generator.js:1134
          var logicResult = Utils.getWhereLogic(logic, hash[key][logic]);
                                                                ^
   TypeError: Cannot read property 'in' of undefined

What am I doing wrong?

I've used before the "in" expression, for example here:

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

And it worked just fine.

I've never used the in expression with a sub-array though, as in this case. So I don't know if thats the correct way to do it.

5 Answers 5

226

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

Tag.findAll({
    where: {
        id: [1,2,3,4]
    }
}).then(...)
Sign up to request clarification or add additional context in comments.

4 Comments

My last piece of code in my question (the one that you are "fixing") is already ok. Please read the entire post:)
Enxtur, I tried it with findAll and only this way all the records with an id in that array were displayed. Otherwise, only the record with the value of the id equal to the value of the first element in the array will be displayed. Thank you for your help! It was very useful! Tag.findAll({ where: { id: [0,1,2,3] } }).then( tag => { res.json( tag ); });
@Emanuelacolta It was misunderstood question and wrong answer, cause my bad english. But i'm happy for it is useful to someone :)
other query its work but some condition in my case i got problem with naming export excel because using to long name by filter on in expression then i didnt catch on generate an excel file. ah my bad. i think its not work .
36

Updated example for Sequelize v5:

await Tag.findAll({
  where: {
    id: {
      [Sequelize.Op.in]: [1, 2, 3, 4]
    }
  }
});

2 Comments

Just make sure it's static method not object accessible method.
It's worth noting the Shorthand syntax for Op.in section with example: id: [1,2,3] // Same as using id: { [Op.in]: [1,2,3] }
1

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) {

    ...
});

Comments

0

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

1 Comment

This does not work in sequelize version 6.
-7

replace in with $in

where : { id : { $in : [1,2,3,4]}}

2 Comments

The above contributor (Jaddu) has confused the syntax in their answer with the similar $in operator for MongoDB. It is NOT the same as the in operator that Sequelize offers, and the answer provided by Enxtur above, which eliminates the operator altogether in favour of an array value is the correct solution to the problem.
They aren't confusing it with MongoDB and their answer was still somewhat reasonable in 2018. sequelize.org/v4/manual/tutorial/querying.html "For backward compatibility reasons Sequelize sets the following aliases by default - $eq, $ne, $gte, $gt, $lte, $lt, $not, $in, $notIn…" I'm here looking for something else, but that stuck out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.