Skip to content

Commit faf96a0

Browse files
committed
Don't return lastID when primary key is TEXT type
1 parent e8fb022 commit faf96a0

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/sqlite3.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ SQLite3.prototype.disconnect = function disconnect(cb) {
286286
};
287287

288288
SQLite3.prototype.getInsertedId = function(model, info) {
289-
return info.lastID;
289+
if (!this._isPrimaryKeyText(model)) {
290+
return info.lastID;
291+
}
290292
};
291293

292294
/**
@@ -366,6 +368,18 @@ SQLite3.prototype._buildColumnDefinition = function(model, propertyName) {
366368
return line;
367369
};
368370

371+
SQLite3.prototype._isPrimaryKeyText = function(model) {
372+
var properties = this.getModelDefinition(model).properties;
373+
for (var propertyName in properties) {
374+
if (!properties.hasOwnProperty(propertyName)) continue;
375+
var property = this.getModelDefinition(model).properties[propertyName];
376+
if (property.id && this._columnDataType(model, propertyName) === 'TEXT') {
377+
return true;
378+
}
379+
}
380+
return false;
381+
};
382+
369383
function _convertBoolean(value) {
370384
var booleanTrue = ['t', 'T', 'y', 'Y', '1', 1, true];
371385
var booleanFalse = ['f', 'F', 'n', 'N', '0', 0, false];

test/sqlite.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require('./init');
1010
var should = require('should');
1111

1212
var Post;
13+
var ModelWithPKString;
1314
var db;
1415

1516
/*global describe, before, it, getDataSource*/
@@ -24,11 +25,18 @@ describe('sqlite3 connector', function() {
2425
loc: 'GeoPoint',
2526
approved: Boolean
2627
});
28+
29+
ModelWithPKString = db.define('ModelWithPKString', {
30+
id: {type: String, id: true},
31+
content: {type: String},
32+
});
2733
});
2834

2935
it('should run migration', function(done) {
3036
db.automigrate('PostWithBoolean', function() {
31-
done();
37+
db.automigrate('ModelWithPKString', function() {
38+
done();
39+
});
3240
});
3341
});
3442

@@ -139,6 +147,17 @@ describe('sqlite3 connector', function() {
139147
done();
140148
});
141149
});
150+
151+
it('should return the id inserted when primary key is a string type',
152+
function(done) {
153+
ModelWithPKString.create({id: 'PK_ID_TEST', content: 'content_TEST'},
154+
function(err, p) {
155+
should.not.exists(err);
156+
p.should.have.property('id', 'PK_ID_TEST');
157+
p.should.have.property('content', 'content_TEST');
158+
done();
159+
});
160+
});
142161
});
143162

144163
// FIXME: The following test cases are to be reactivated for PostgreSQL

0 commit comments

Comments
 (0)