使用魔术方法addModel / setModel时,在数据透视表上顺序执行SELECT而不是插入。以前有没有人经历过这种行为?
这里是模型和SQL日志输出的示例。 在模型文件夹下显示index.js还是bin / www sequelize.sync方法对继续进行有帮助?
user.js
'use strict';
const Sequelize = require('sequelize'),
Op = Sequelize.Op;
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
//@BEGIN
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
name: {
type: DataTypes.STRING(256),
allowNull: false,
},
address: {
type: DataTypes.STRING(256),
allowNull: false,
},
//@END
}, {
deletedAt: 'deleted_at',
paranoid: true,
rejectOnEmpty: true,
freezeTableName: true,
tableName: 'users',
});
User.associate = function(models) {
User.belongsToMany(models.Document, {
through: 'documents_users',
as: 'Document',
});
};
return User;
};
document.js “严格使用”;
const Sequelize = require('sequelize'),
Op = Sequelize.Op;
module.exports = (sequelize, DataTypes) => {
const Document = sequelize.define('Document', {
//@BEGIN
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
user_id: {
allowNull: false,
type: DataTypes.INTEGER,
},
type: {
allowNull: false,
type: DataTypes.STRING(32),
},
type_of_identity_card: {
allowNull: false,
type: DataTypes.STRING(32),
},
document_number: {
type: DataTypes.STRING(64),
allowNull: false,
},
expiration: {
type: DataTypes.DATE,
allowNull: false,
},
status: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
path: {
type: DataTypes.STRING(256),
allowNull: false,
},
//@END
}, {
deletedAt: 'deleted_at',
paranoid: true,
rejectOnEmpty: true,
freezeTableName: true,
tableName: 'documents',
});
Document.associate = function(models) {
Document.belongsToMany(models.User, {
through: 'documents_users',
as: 'User'
});
};
Document.prototype.isExpired = function () {
return new Promise((resolve, reject) => {
const
now = new Date(),
expiration = new Date(this.expiration);
return (now >= expiration) ? reject("Document expired") : resolve();
})
};
return Document;
};
魔术方法
someMethod(req, res){
Promise.all([
model.Document.create({
"user_id": 60,
"type": "ID card",
"type_of_identity_card": "nieco",
"document_number": "1234",
"expiration": "2019-02-02",
"status": false,
"path": "/asd/dsa.jpg",
}),
model.User.create({
"name": "ASDASD QWEEWQ",
"address": "olwgmwe 1234",
}),
]).then(results => {
const
Document = results[0],
User = results[1];
// console.log("ASD", util.inspect(User.__proto__.getDocument));
return User.setDocument(Document)
}).then(nieco => {
return res.status(200).send(nieco);
})
.catch(error => {
global.logger.error(error);
return res.status(400).send();
})
},
SQL日志
10|admin-s | Executing (default): INSERT INTO "documents" ("id","user_id","type","type_of_identity_card","document_number","expiration","status","path","created_at","updated_at") VALUES (DEFAULT,60,'ID card','nieco','1234','2019-02-02 00:00:00.000 +00:00',false,'/asd/dsa.jpg','2018-08-07 11:51:43.044 +00:00','2018-08-07 11:51:43.044 +00:00') RETURNING *;
10|admin-s | Executing (default): INSERT INTO "users" ("id","name","address","created_at","updated_at") VALUES (DEFAULT,'ASDASD QWEEWQ','olwgmwe 1234','2018-08-07 11:51:43.047 +00:00','2018-08-07 11:51:43.047 +00:00') RETURNING *;
10|admin-s | Executing (default): SELECT "created_at", "updated_at", "document_id", "user_id" FROM "documents_users" AS "documents_users" WHERE "documents_users"."user_id" = 23;
10|admin-s | 2018-08-07T11:51:43.082Z - error: message=, stack=SequelizeEmptyResultError
10|admin-s | at Promise.try.then.then.then.tap.then.results (D:\Projects\01authserver4\admin-service\node_modules\sequelize\lib\model.js:1618:17)
10|admin-s | at runCallback (timers.js:789:20)
10|admin-s | at tryOnImmediate (timers.js:751:5)
10|admin-s | at processImmediate [as _immediateCallback] (timers.js:722:5)
10|admin-s | From previous event:
10|admin-s | at Function.findAll (D:\Projects\01authserver4\admin-service\node_modules\sequelize\lib\model.js:1609:8)
10|admin-s | at BelongsToMany.set (D:\Projects\01authserver4\admin-service\node_modules\sequelize\lib\associations\belongs-to-many.js:526:38)
10|admin-s | at model.obj.(anonymous function) [as setDocument] (D:\Projects\01authserver4\admin-service\node_modules\sequelize\lib\associations\helpers.js:69:40)
10|admin-s | at Promise.all.then.results (D:\Projects\01authserver4\admin-service\app\controller\document.js:98:25)
10|admin-s | at <anonymous>, name=SequelizeEmptyResultError, __stackCleaned__=true
10|admin-s | POST /document/nieco 400 68.909 ms - -