Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(lib/adapters/mysql): fix transaction insert error
  • Loading branch information
mapleincode committed Mar 23, 2018
1 parent 4bae14e commit bce7cad
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/adapters/mysql.js
Expand Up @@ -568,7 +568,7 @@ class MySQLAdapter extends Adapter {
sql += ` ${index}`;
}
}

sql += ` SET ${set}`;

if(options.where && Object.keys(options.where).length) {
Expand Down Expand Up @@ -680,7 +680,7 @@ class MySQLAdapter extends Adapter {
this.execute(options.conn, sql, function(err, rows) {
if(err) {
return callback(err, undefined, sql);
}
}

if(options.single) {
return callback(undefined, (rows || []).length ? rows[0] : null, sql);
Expand Down Expand Up @@ -983,7 +983,7 @@ class MySQLAdapter extends Adapter {
options.fields = primaryKeys;

let relatedSql;

try {
relatedSql = self.makeSql("find", query.model, options);
} catch(e) {
Expand Down Expand Up @@ -1018,7 +1018,7 @@ class MySQLAdapter extends Adapter {
* @param {Object} data data to be inserted
* @param {Function} callback the callback function
*/
insert(model, conn, data, callback) {
insert(model, conn, data, callback) {
// fill data into sql string
//
// INSERT INTO `name` SET =>
Expand Down Expand Up @@ -1056,14 +1056,14 @@ class MySQLAdapter extends Adapter {
// query the instance from database now
if(row.insertId) {
// if this row has an auto increament id

if(primaryKeys.length === 1) {
// if we have only one primary key
//
// 1. no AI specified in model: consume primary is AI in database, use AI as primary key to query
// 2. primary key is AI: use AI as primary key to query
// 3. primary key is not AI: use primary key to query

if(!autoIncrement || autoIncrement.primaryKey) {
where[primaryKeys[0].name] = row.insertId;
} else {
Expand All @@ -1077,7 +1077,7 @@ class MySQLAdapter extends Adapter {
//
// step 2. (only have auto increament key)
// overwrite the AI to the certain primary key

// --> step 1.
where = primaryValues;

Expand All @@ -1104,7 +1104,12 @@ class MySQLAdapter extends Adapter {
where = primaryValues;
}

model.where(where).findOne(function(err, row) {
let _model = model;
if(conn) {
_model = _model.conn(conn);
}

_model.where(where).findOne(function(err, row) {
if(err) {
return callback(err, undefined, sql);
}
Expand Down

0 comments on commit bce7cad

Please sign in to comment.