diff --git a/lib/sqlite3.js b/lib/sqlite3.js index fe56159..1743e8a 100644 --- a/lib/sqlite3.js +++ b/lib/sqlite3.js @@ -286,7 +286,9 @@ SQLite3.prototype.disconnect = function disconnect(cb) { }; SQLite3.prototype.getInsertedId = function(model, info) { - return info.lastID; + if (this._isPrimaryKeyInteger(model)) { + return info.lastID; + } }; /** @@ -366,6 +368,18 @@ SQLite3.prototype._buildColumnDefinition = function(model, propertyName) { return line; }; +SQLite3.prototype._isPrimaryKeyInteger = function(model) { + var properties = this.getModelDefinition(model).properties; + for (var propertyName in properties) { + if (!properties.hasOwnProperty(propertyName)) continue; + var property = this.getModelDefinition(model).properties[propertyName]; + if (property.id) { + return this._columnDataType(model, propertyName) === 'INTEGER'; + } + } + return false; +}; + function _convertBoolean(value) { var booleanTrue = ['t', 'T', 'y', 'Y', '1', 1, true]; var booleanFalse = ['f', 'F', 'n', 'N', '0', 0, false]; diff --git a/test/sqlite.test.js b/test/sqlite.test.js index d470282..5fc43f2 100644 --- a/test/sqlite.test.js +++ b/test/sqlite.test.js @@ -10,6 +10,7 @@ require('./init'); var should = require('should'); var Post; +var ModelWithPKString; var db; /*global describe, before, it, getDataSource*/ @@ -24,11 +25,16 @@ describe('sqlite3 connector', function() { loc: 'GeoPoint', approved: Boolean }); + + ModelWithPKString = db.define('ModelWithPKString', { + id: {type: String, id: true}, + content: {type: String}, + }); }); it('should run migration', function(done) { - db.automigrate('PostWithBoolean', function() { - done(); + db.automigrate(['PostWithBoolean', 'ModelWithPKString'], function(err) { + done(err); }); }); @@ -139,6 +145,27 @@ describe('sqlite3 connector', function() { done(); }); }); + + it('should return the id inserted when primary key is a string type', + function(done) { + ModelWithPKString.create({id: 'PK_ID_TEST', content: 'content_TEST'}, + function(err, p) { + should.not.exists(err); + p.should.have.property('id', 'PK_ID_TEST'); + p.should.have.property('content', 'content_TEST'); + done(); + }); + }); + + it('should return 0 documents when filtering with non existing field', + function(done) { + Post.count({nonexistingfield: '__TEST__'}, + function(err, count) { + should.not.exists(err); + count.should.equal(0); + done(); + }); + }); }); // FIXME: The following test cases are to be reactivated for PostgreSQL