Repository navigation
Description
hi there,
I think the following is a bug , most people will not notice it happening (it's non-destructive) but in my case I am triggering side-effects in "after update" hooks (with individualHooks turned on - although my tests show that the individualHooks option has no bearing on this problem) which I really only want if some record(s) is/are actually mutated.
I am using the Sequelize v4 beta but from looking at the relevant source I think the described behaviour has been around for a while!
What you are doing?
Performing a Model.update(fields, options) call with a fields object that contains no keys that are valid fields/attributes.
The following function can be run to reproduce the problem, please call it with an instance of Sequelize:
function runtest(db)
{
/* SETUP ... */
var TEU = db.define('TestEmptyUpdate', {
name : db.Sequelize.DataTypes.STRING,
type : db.Sequelize.DataTypes.STRING
});
var data = { name : 'untested' + (+new Date), type : 'foo' }
var junk = { not_a_field : true, also_not_a_field : 'really' };
return TEU
.sync()
.then(function() { return TEU.create(data, { logging : logsql }); })
/**
* TEST: updating via model.update() with INVALID fields (does not trigger a query - I feel this is INCORRECT)
*/
.then(function(inst) { return TEU.update(junk, { where : { id : inst.id }, logging : logsql }); })
/**
* CONTROL: updating instance with INVALID fields (does not trigger a query - I feel this is correct)
*/
.then (function() { return TEU.findOne(); })
.then (function(inst) { return inst.update(junk, { logging : logsql }); })
/**
* CONTROL: updating instance with VALID fields (does trigger a query - this is definitely correct :-)
*/
.then (function() { return TEU.findOne(); })
.then (function(inst) { return inst.update( { name : 'tested' }, { logging : logsql }); })
.catch(console.error)
.then (function() { return TEU.drop(); })
.then (function() { process.exit(); })
.catch(function(e) { console.error(e); process.exit(1); })
;
}What do you expect to happen?
I expect the query not to occur when no valid fields have been specified for update (updated_at, created_at, etc should only be added to the list of fields to update if that list is not empty (empty after having been filtered for invalid fields/attributes), Model.update() should resolve and give ZERO for the "affected rows" value.
What is actually happening?
Sequelize is running a query like the following (when running TEU.update(junk, ...) in the code above):
UPDATE `TestEmptyUpdates` SET `updatedAt`='2017-01-11 13:40:57' WHERE `id` = 1Dialect: mysql
Database version: Ver 14.14 Distrib 5.7.16, for osx10.10
Sequelize version: v4.0.0-2
Activity
shardool-freightwalla commentedon Apr 10, 2017
Facing the same issue
stale commentedon Jun 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
iamjochem commentedon Jun 29, 2017
this is still a thing
stale commentedon Aug 28, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
iamjochem commentedon Aug 28, 2017
hi stale-bot a lack of activity is a symtom of lack of contributor time/resources not an indication that the bug has magically fixed itself :-)
sdotson commentedon Nov 3, 2017
yes, still seeing this bug.
zupper commentedon Apr 23, 2019
The same behavior is still observable in 5.7.3. Noticing the below log, where my updatedAt column is aliased to updated in the model definition's options:
2019/04/23 12:12:49 Executing (817790ac-3497-4294-b399-f85c0608cef4): UPDATE `Event` SET `updated`=? WHERE `id` = ?zupper commentedon Apr 23, 2019
Correction to the above - the same problem exists when saving through the instance, not statically.