You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
undefined fields in where options are treated as NULL #8583
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
Reacted by Youngrok Kim, dcoun, Joseph Mansfield, Andreas Kleiber, Rovack, Jens Fredskov, Tim Golovchak, konstantinbarkalov, nickkeane, Victor Villas and 3 more
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.
Reacted by Chris Butler, Youngrok Kim, Maiquel Leonel, Christopher Carman, nickkeane, Kais Ben Salah, Formica, Abdelellah, Gulg and Patrick Strittmatter
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.
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:
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.
@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
Reacted by Marc Schipperheyn and Muhammad-Sabbir-Tech
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!
Activity
sushantdhiman commentedon Nov 9, 2017
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
undefinedasnull, I can think ofupdateatleastgabegorelick commentedon Nov 9, 2017
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.rokoroku commentedon Dec 7, 2017
I totally agree with
undefined != nulltoo.We can also add a flag like
treatUndefinedAsNullto the global configuration object. It won't cause a big change.leahciMic commentedon Jan 31, 2018
I think this might actually be a dangerous route...
Imagine this in a destroy scenario:
Currently, if
comment.idisundefinedSequelize 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
keyis defined but isundefined, then treat it likenull. 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:
And use it where necessary?
gabegorelick commentedon Mar 2, 2018
Fair point, but you could argue you still have this problem if
idis 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).That is indeed what we do. But it's cumbersome to have to filter all our
whereoptions throughout the app when this should arguably be the default behavior.Alexsey commentedon Apr 25, 2018
Hm. How can you achieve this behavior? If I'll execute code
I'll get in MySQL
And in SQL Server
Note
= NULLand notIS NULLAre you sure that issue is still actual?
Alexsey commentedon May 2, 2018
@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
eseliger commentedon May 2, 2018
I think it was more a bug than a feature, so I am totally up for fixing it for the v5 release.
undefinedis definitely not the same asnullin javascript.=> PRs welcome, if others agree as well!
mschipperheyn commentedon May 17, 2018
Just ran into this bug. Would be in favor of a 4.x fix + option in sequelize options to activate the "fixed" behavior
sushantdhiman commentedon Jun 20, 2018
Please give your comments on implementation #9548
We are going to ignoreundefinedfor all queries exceptDELETE. ForDELETEsequelize will simply throw an error