Permalink
Browse files

fixed return wrong id value without auto increment.

  • Loading branch information...
taoyuan
taoyuan committed Jul 22, 2013
1 parent bb614f2 commit 333e86539650a3b2c38fec72bc50aa3befedcb7c
Showing with 28 additions and 3 deletions.
  1. +4 −2 lib/connection.js
  2. +24 −1 test/insert.js
View
@@ -111,8 +111,10 @@ var Connection = Class.extend({
return;
}
if (isNew && data.lastId) {
- var idPropName = obj._getModel().getIdPropertyName();
- obj[idPropName] = data.lastId;
+ var idPropName = obj._getModel().getIdPropertyName();
+ if (!obj[idPropName] || obj._getModel().columns[idPropName].autoIncrement) {
+ obj[idPropName] = data.lastId;
+ }
}
obj._getConnection = function() { return self; }; // hide from JSON.stringify
obj._isPersisted = function() { return true; };
View
@@ -13,7 +13,7 @@ exports['Insert'] = nodeunit.testCase({
});
this.PrimaryKeyTest = persist.define("PrimaryKeyTest", {
- "id": { dbColumnName: 'my_pk_id' },
+ "id": { dbColumnName: 'my_pk_id', primaryKey: true },
"name": type.STRING
});
@@ -57,6 +57,29 @@ exports['Insert'] = nodeunit.testCase({
callback();
},
+ "primary key set customized": function (test) {
+ var self = this;
+ var ID = 1001,
+ item = new this.PrimaryKeyTest({id: ID, name: 'item-1001'});
+ self.connection.save(item, function (err, obj) {
+ if (err) {
+ return console.log(err);
+ }
+
+ test.equals(obj.id, ID);
+
+ self.PrimaryKeyTest.all(self.connection, function (err, qItems) {
+ if (err) {
+ return console.log(err);
+ }
+ test.equals(qItems.length, 1);
+ test.equals(qItems[0].id, ID);
+
+ test.done();
+ });
+ });
+ },
+
"primary key not named id": function (test) {
var self = this;
var item1 = new this.PrimaryKeyTest({name: 'item1'});

0 comments on commit 333e865

Please sign in to comment.