Missing primary key value after create
goodman3 opened this issue · 0 comments
goodman3 commented
Version:
package:
"dependencies": {
"bluebird": "^3.5.1",
"mysql": "^2.15.0",
"orm": "^4.0.2",
"orm-transaction": "0.0.2",
"request": "^2.83.0",
"validator": "^8.2.0"
}
node -v:
v6.10.2
db driver:
ORM.connectAsync({
host: obj.host,
user: obj.user,
password: obj.password,
database: obj.databaseName,
protocol: 'mysql',
port: obj.port,
query: {
debug: isDebug,
// timezone : 'GMT+8'
}
})
Model:
const user = require('../user');
const schedule = db.define('schedule', {
id: {type: 'serial', key: true},
title: {type: 'text', size: 128, key: true},
text: {type: 'text', size: 512},
is_todo_list: {type: 'boolean', index: true,defaultValue:false},
type: {type: 'enum', values: ['SELF', 'BROADCAST', 'OTHER'], index: true, required: true},
created_at: {type: 'date', time: true, index: true},
start_at: {type: 'date', time: true, index: true},
end_at: {type: 'date', time: true, index: true},
}, {
hooks: {
afterAutoFetch: function () {
this.created_at = new Date(this.created_at).getTime();
this.start_at = new Date(this.start_at).getTime();
this.end_at = new Date(this.end_at).getTime();
},
beforeValidation: function () {
this.created_at = new Date(this.created_at);
this.start_at = new Date(this.start_at);
this.end_at = new Date(this.end_at);
}
}
});
schedule.hasMany('receiver', user);
Create:
console.log('obj :',obj);
schedule.create(obj, (e, r) => {
console.log('e,r :',e,JSON.stringify(r));
if (e) {
reject(e);
} else {
resolve(r);
}
});
And console log shows:
obj : { title: 'title',
text: 'text',
is_todo_list: true,
type: 'OTHER',
start_at: 1517917402000,
end_at: 1517967402000,
receiver: [ 2, 3 ] }
[SQL/mysql] INSERT INTO `schedule` (`title`, `text`, `is_todo_list`, `type`, `start_at`, `end_at`, `created_at`, `author_id`) VALUES ('title', 'text', 1, 'OTHER', '2018-02-06 19:43:22.000', '2018-02-07 09:36:42.000', '1970-01-01 08:00:00.000', NULL)
[SQL/mysql] SELECT `t1`.`id`, `t1`.`name`, `t1`.`level`, `t1`.`master_id` FROM `department` `t1` JOIN `user_department` `t2` ON `t2`.`department_id` = `t1`.`id` WHERE `t2`.`user_id` = 2
[SQL/mysql] SELECT `t1`.`id`, `t1`.`name`, `t1`.`level`, `t1`.`master_id` FROM `department` `t1` JOIN `user_department` `t2` ON `t2`.`department_id` = `t1`.`id` WHERE `t2`.`user_id` = 3
[SQL/mysql] DELETE FROM `schedule_receiver` WHERE `schedule_id` IS NULL AND `undefined` = 'title'
e,r : { Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'where clause'
at Query.Sequence._packetToError (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Query.ErrorPacket (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (D:\projects\xxxxxxx\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
--------------------
at Protocol._enqueue (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Connection.query (D:\projects\xxxxxxx\node_modules\mysql\lib\Connection.js:208:25)
at Driver.execSimpleQuery (D:\projects\xxxxxxx\node_modules\orm\lib\Drivers\DML\mysql.js:103:13)
at Driver.remove (D:\projects\xxxxxxx\node_modules\orm\lib\Drivers\DML\mysql.js:216:8)
at run (D:\projects\xxxxxxx\node_modules\orm\lib\Associations\Many.js:339:25)
at Object.value (D:\projects\xxxxxxx\node_modules\orm\lib\Associations\Many.js:352:9)
at Object.value (D:\projects\xxxxxxx\node_modules\orm\lib\Associations\Many.js:298:40)
at saveAssociation (D:\projects\xxxxxxx\node_modules\orm\lib\Instance.js:279:25)
at _saveManyAssociation (D:\projects\xxxxxxx\node_modules\orm\lib\Instance.js:332:7)
at saveAssociations (D:\projects\xxxxxxx\node_modules\orm\lib\Instance.js:336:7)
at D:\projects\xxxxxxx\node_modules\orm\lib\Instance.js:218:14
at Query._callback (D:\projects\xxxxxxx\node_modules\orm\lib\Drivers\DML\mysql.js:196:12)
at Query.Sequence.end (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Query._handleFinalResultPacket (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
at Query.OkPacket (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\sequences\Query.js:72:10)
at Protocol._parsePacket (D:\projects\xxxxxxx\node_modules\mysql\lib\protocol\Protocol.js:279:23)
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlMessage: 'Unknown column \'undefined\' in \'where clause\'',
sqlState: '42S22',
index: 0,
sql: 'DELETE FROM `schedule_receiver` WHERE `schedule_id` IS NULL AND `undefined` = \'title\'',
instance:
{ id: [Getter/Setter],
title: [Getter/Setter],
text: [Getter/Setter],
is_todo_list: [Getter/Setter],
type: [Getter/Setter],
created_at: [Getter/Setter],
start_at: [Getter/Setter],
end_at: [Getter/Setter],
author_id: [Getter/Setter],
receiver: [Getter/Setter] } } undefined
If I remove hasMany object 'receiver: [ 2, 3 ]', the result will be:
obj : { title: 'title',
text: 'text',
is_todo_list: true,
type: 'OTHER',
start_at: 1517917402000,
end_at: 1517967402000 }
[SQL/mysql] INSERT INTO `schedule` (`title`, `text`, `is_todo_list`, `type`, `start_at`, `end_at`, `created_at`, `author_id`) VALUES ('title', 'text', 1, 'OTHER', '2018-02-06 19:43:22.000', '2018-02-07 09:36:42.000', '1970-01-01 08:00:00.000', NULL)
e,r : null {"title":"title","text":"text","is_todo_list":true,"type":"OTHER","created_at":"1970-01-01T00:00:00.000Z","start_at":"2018-02-06T11:43:22.000Z","end_at":"2018-02-07T01:36:42.000Z","author_id":null}
You can see after create, id is missing. I can't understand why because my other models are more complicated but they don't have this issue.