Skip to content

Commit 45a32e1

Browse files
committed
Fixed issue with insertId being returned for models with no auto-incrementing field. See http://stackoverflow.com/questions/22673127/returned-objects-id-is-zero-creating-a-record-in-database
1 parent ddd7420 commit 45a32e1

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/adapter.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,20 @@ module.exports = (function() {
300300
if (err) return cb( handleQueryError(err) );
301301

302302
// Build model to return
303-
var autoInc = 'id';
303+
var autoInc = null;
304304

305305
Object.keys(collection.definition).forEach(function(key) {
306306
if(!collection.definition[key].hasOwnProperty('autoIncrement')) return;
307307
autoInc = key;
308308
});
309-
309+
310310
var autoIncData = {};
311-
autoIncData[autoInc] = result.insertId;
311+
312+
if (autoInc) {
313+
314+
autoIncData[autoInc] = result.insertId;
315+
316+
}
312317

313318
var model = _.extend({}, data, autoIncData);
314319

0 commit comments

Comments
 (0)