Skip to content

undefined fields in where options are treated as NULL #8583

Closed

Description

@gabegorelick

What are you doing?

User.findAll({
  where: {
    name: req.query.name
  }
});

What do you expect to happen?

If name param wasn't passed, then this should find all users.

What is actually happening?

SELECT ... FROM users WHERE name IS NULL, i.e. if a field in where is undefined, it's treated as NULL.

This is especially confusing because it treats undefined in Javascript two different ways. If the value is present but undefined, then it's NULL. But if it's missing and undefined, then it's obviously not included in the WHERE clause at all. This means that consumers can't rely on where.name !== undefined to know whether a clause will be included. Instead you have to use the relatively obscure in operator.

Dialect: any, but tested on Postgres
Dialect version: any
Database version: any
Sequelize version: master
Tested with master branch: Yes

Activity

  1. added
    status: in discussion
    on Nov 9, 2017
  2. sushantdhiman commented on Nov 9, 2017

    @sushantdhiman
    Contributor

    This is a point of discussion, I mostly agree undefined != null. This is going to be a breaking change.

    We also need to find other API that may also treat undefined as null, I can think of update atleast

  3. gabegorelick commented on Nov 9, 2017

    @gabegorelick
    ContributorAuthor

    Yeah this will be a huge breaking change. It may be wise to hide it behind an option first (something like Model.find({where: {foo: undefined}, treatUndefinedAsNull: false}) and then change the default behavior in a future major version release.

  4. rokoroku commented on Dec 7, 2017

    @rokoroku
    Contributor

    I totally agree with undefined != null too.
    We can also add a flag like treatUndefinedAsNull to the global configuration object. It won't cause a big change.

  5. leahciMic commented on Jan 31, 2018

    @leahciMic

    I think this might actually be a dangerous route...

    Imagine this in a destroy scenario:

    comments.destroy({
      where: {
        id: comment.id,
        userId: user.id,
      },
    });

    Currently, if comment.id is undefined Sequelize wont destroy every comment belonging to a particular user. I think the current working way is much safer and a much saner default.

    I can't see any good reason to make this change.

    If a key is defined but is undefined, then treat it like null. This change could result in unintentional loss of data through application errors and the like.

    @gabegorelick Could you not write/ find a function that recursively strips undefined? Here is a poor man's version:

    function stripUndefined(obj) {
      return JSON.parse(JSON.stringify(obj));
    }

    And use it where necessary?

  6. gabegorelick commented on Mar 2, 2018

    @gabegorelick
    ContributorAuthor

    Currently, if comment.id is undefined Sequelize wont destroy every comment belonging to a particular user.

    Fair point, but you could argue you still have this problem if id is omitted entirely. If you're really concerned about accidentally deleting everything, there are a lot of alternatives (LIMIT, enforcing filters in hooks or a DAO layer, etc).

    Could you not write/ find a function that recursively strips undefined? Here is a poor man's version

    That is indeed what we do. But it's cumbersome to have to filter all our where options throughout the app when this should arguably be the default behavior.

  7. Alexsey commented on Apr 25, 2018

    @Alexsey
    Contributor

    Hm. How can you achieve this behavior? If I'll execute code

    const Sequelize = require('sequelize')
    
    const sequelize = new Sequelize(
        'db', 'root', 'root', {
            host: 'localhost',
            dialect: 'mysql',
            pool: {max: 100, min: 0, idle: 10000},
            dialectOptions: {
                charset: 'utf8_general_ci',
            },
        }
    )
    
    ;(async () => {
        await sequelize.authenticate()
    
        const User = sequelize.define('User', {
            id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
            name: {type: Sequelize.STRING, allowNull: true}
        })
    
        await sequelize.sync({force: true})
    
        await User.findAll({where: {name: undefined}})
    })()

    I'll get in MySQL

    SELECT `id`, `name`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`name` = NULL;

    And in SQL Server

    SELECT [id], [name], [createdAt], [updatedAt] FROM [User] AS [User] WHERE [User].[name] = NULL;

    Note = NULL and not IS NULL

    Are you sure that issue is still actual?

  8. Alexsey commented on May 2, 2018

    @Alexsey
    Contributor

    @sushantdhiman @janmeier @eseliger could you, please, consider to include this in the v5 scope? Otherwise, we would need to wait for another year or so before v6

  9. eseliger commented on May 2, 2018

    @eseliger
    Member

    I think it was more a bug than a feature, so I am totally up for fixing it for the v5 release. undefined is definitely not the same as null in javascript.
    => PRs welcome, if others agree as well!

  10. mschipperheyn commented on May 17, 2018

    @mschipperheyn

    Just ran into this bug. Would be in favor of a 4.x fix + option in sequelize options to activate the "fixed" behavior

  11. sushantdhiman commented on Jun 20, 2018

    @sushantdhiman
    Contributor

    Please give your comments on implementation #9548

    We are going to ignore undefined for all queries except DELETE. For DELETE sequelize will simply throw an error

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

    status: in discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions