Hello, I have a problem with the method "increment".
Here is a simple example rather than a long speech:
var Option = sequelize.define('Option', {
id: {
field: 'id',
type: Sequelize.INTEGER(10).UNSIGNED,
primaryKey: true,
allowNull: false,
autoIncrement: true,
},
key: {
field: 'key',
type: Sequelize.STRING(60),
primaryKey: false,
allowNull: false,
defaultValue: "",
autoIncrement: false,
},
value: {
field: 'value',
type: Sequelize.STRING(1000),
primaryKey: false,
allowNull: false,
defaultValue: "",
autoIncrement: false,
},
createdAt: {
field: 'created_at',
type: Sequelize.DATE,
primaryKey: false,
autoIncrement: false,
},
updatedAt: {
field: 'updated_at',
type: Sequelize.DATE,
primaryKey: false,
autoIncrement: false,
},
}, {
tableName: 'option',
freezeTableName: true,
});
Option.find({
key: 'number_visitor',
}).then(function(option) {
option.increment({
value: 1,
}).then(function() {
console.log('Ok =)');
});
});
Console :
D:\Valentin\Node\Sequelize>node increment.js
Executing (default): SELECT `id`, `key`, `value`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt` FROM `option` AS `Option` LIMIT 1;
Executing (default): UPDATE `option` SET `value`=`value` + 1,`updatedAt`='2015-11-16 11:57:02' WHERE `id` = 1
Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'updatedAt' in 'field list'
at Query.formatError (D:\Valentin\Node\node_modules\sequelize\lib\dialects\mysql\query.js:160:14)
at Query._callback (D:\Valentin\Node\node_modules\sequelize\lib\dialects\mysql\query.js:35:21)
at Query.Sequence.end (D:\Valentin\Node\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Query.ErrorPacket (D:\Valentin\Node\node_modules\mysql\lib\protocol\sequences\Query.js:94:8)
at Protocol._parsePacket (D:\Valentin\Node\node_modules\mysql\lib\protocol\Protocol.js:274:23)
at Parser.write (D:\Valentin\Node\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (D:\Valentin\Node\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (D:\Valentin\Node\node_modules\mysql\lib\Connection.js:96:28)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
From previous event:
at Query.run (D:\Valentin\Node\node_modules\sequelize\lib\dialects\mysql\query.js:30:17)
at D:\Valentin\Node\node_modules\sequelize\lib\sequelize.js:771:18
From previous event:
at Promise.then (D:\Valentin\Node\node_modules\sequelize\lib\promise.js:21:17)
at Object.<anonymous> (D:\Valentin\Node\Sequelize\increment.js:50:4)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
We can see that generated SQL query uses the field "createdAt" by default instead of using this of the model ("updated_at"). Whereas in the query "find" it does well the conversion of the field ("createdAt" to "updated_at").
The only way to temporarily fix the problem is to use the "underscored" to "true" in the declaration of the model.
Most helpful comment
Fixed by https://github.com/sequelize/sequelize/pull/6810