Permalink
Browse files

fix: fix #38

  • Loading branch information...
XadillaX committed Feb 29, 2016
1 parent cb8a871 commit bdde1074cfd4c04a6163a9fbacb1fc19f47fa709
Showing with 23 additions and 4 deletions.
  1. +23 −4 lib/yukari.js
@@ -303,11 +303,30 @@ Yukari.prototype.insert = function(callback) {

var where = {};
var pk = self.$model.primaryKeys;
var ai = self.$model.ai;
if(row.insertId) {
where[pk[0].name] = row.insertId;
} else {
for(var i = 0; i < self.$model.primaryKeys.length; i++) {
where[pk[0].name] = self[pk[0].name];
// 只有一个主键
if(pk.length === 1) {
if(!ai || ai.primaryKey) {
// Model 没定义过 AI,或者定义的 AI 就是主键
where[pk[0].name] = row.insertId;
} else {
// Model 定义过 AI 且不是主键
where[pk[0].name] = (self[pk[0].name] !== undefined) ?
self[pk[0].name] : self[pk[0].column];
}
} else {
// 有多个主键

for(var i = 0; i < self.$model.primaryKeys.length; i++) {
where[pk[0].name] = (self[pk[i].name] !== undefined) ?
self[pk[i].name] : self[pk[i].column];
}

// 如果有 AI 主键那么覆盖一下
if(ai && ai.primaryKey) {
self[ai.name] = row.insertId;
}
}
}

0 comments on commit bdde107

Please sign in to comment.