diff --git a/lib/adapters/mysql.js b/lib/adapters/mysql.js index bf0e9ce..c84be4b 100644 --- a/lib/adapters/mysql.js +++ b/lib/adapters/mysql.js @@ -568,7 +568,7 @@ class MySQLAdapter extends Adapter { sql += ` ${index}`; } } - + sql += ` SET ${set}`; if(options.where && Object.keys(options.where).length) { @@ -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); @@ -983,7 +983,7 @@ class MySQLAdapter extends Adapter { options.fields = primaryKeys; let relatedSql; - + try { relatedSql = self.makeSql("find", query.model, options); } catch(e) { @@ -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 => @@ -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 { @@ -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; @@ -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); }