Permalink
Browse files

Merge pull request #83 from torworx/master

fixed runSqlFromFile self undefined exception and primary key without auto increment always returns wrong integer after save
  • Loading branch information...
2 parents b449b19 + e30b5cf commit ed36345ebea8bc60fe6657a2e7046411a33cd408 @joeferner committed Jul 22, 2013
Showing with 29 additions and 3 deletions.
  1. +5 −2 lib/connection.js
  2. +24 −1 test/insert.js
View
7 lib/connection.js
@@ -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; };
@@ -316,6 +318,7 @@ var Connection = Class.extend({
// filename, callback
runSqlFromFile: over([
[over.string, over.arrayOptionalWithDefault(null), over.callbackOptional, function(filename, params, callback) {
+ var self = this;
this._readSqlFile(filename, function(err, sql) {
if (err) {
return callback(err);
View
25 test/insert.js
@@ -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 without auto increment": 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 ed36345

Please sign in to comment.