Skip to content

Don't return lastID when primary key is TEXT type solve #13 #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

/**
Expand Down Expand Up @@ -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];
Expand Down
31 changes: 29 additions & 2 deletions test/sqlite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require('./init');
var should = require('should');

var Post;
var ModelWithPKString;
var db;

/*global describe, before, it, getDataSource*/
Expand All @@ -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);
});
});

Expand Down Expand Up @@ -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
Expand Down