Skip to content

$notIn empty array #4859

Closed
Closed

Description

@markushedlund

Hi!

where: { id: { $notIn: [] } } produces WHERE id NOT IN (NULL) which returns nothing. Shouldn't that Sequelize query logically return every row? :)

Tested with 3.9 and 3.13.

Activity

  1. markushedlund commented on Nov 13, 2015

    @markushedlund
    ContributorAuthor

    Created a pull request #4860. But I realize this would be a breaking change.

  2. jesseditson commented on Jan 26, 2016

    @jesseditson

    Just ran in to this (using postgres), and was pretty tripped up by it. Thanks for the PR, and hopefully this will make it in so I can remove the hacks on my side!

  3. added
    breaking change
    on Jan 26, 2016
  4. added this to the 4.0 milestone on Jan 26, 2016
  5. markushedlund commented on Jan 27, 2016

    @markushedlund
    ContributorAuthor

    Workaround:

    var _ = require('lodash');
    var QueryGenerator = require('../node_modules/sequelize/lib/dialects/abstract/query-generator');
    var whereItemQuery = QueryGenerator.whereItemQuery;
    
    QueryGenerator.whereItemQuery = function(key, value, options) {
        if (value && value.$notIn && _.isArray(value.$notIn) && !value.$notIn.length) {
            return '';
        } else {
            return whereItemQuery.apply(this, arguments);
        }
    };
    
  6. vineshhpatel commented on Oct 26, 2017

    @vineshhpatel

    for < 4 version, here is an alternative work around,

    let idsToSkip = [...]; //generate array of ids, this may result in empty array
    idsToSkip.push('-1'); // push a value that can not be the id in table. this will generate `not in ('-1')` query serving the purpose.
    db.foo.findAll({
      where: {
        id: {
          $notIn: idsToSkip
        }
      }
    });
    

    check it out here, https://stackoverflow.com/a/46947550/842386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking change

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions