diff --git a/lib/model-definition.js b/lib/model-definition.js index 32bbcd1c..21094812 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -141,7 +141,7 @@ ModelDefinition.prototype.ids = function () { ids.push({name: key, id: id, property: props[key]}); } ids.sort(function (a, b) { - return a.key - b.key; + return a.id - b.id; }); this._ids = ids; return ids; diff --git a/test/model-definition.test.js b/test/model-definition.test.js index 342dfde3..b44fd971 100644 --- a/test/model-definition.test.js +++ b/test/model-definition.test.js @@ -215,6 +215,28 @@ describe('ModelDefinition class', function () { done(); }); + it('should sort id properties by its index', function () { + var modelBuilder = new ModelBuilder(); + + var User = new ModelDefinition(modelBuilder, 'User', { + userId: {type: String, id: 2}, + userType: {type: String, id: 1}, + name: "string", + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: "number" + }); + + var ids = User.ids(); + assert.ok(Array.isArray(ids)); + assert.equal(ids.length, 2); + assert.equal(ids[0].id, 1); + assert.equal(ids[0].name, 'userType'); + assert.equal(ids[1].id, 2); + assert.equal(ids[1].name, 'userId'); + }); + it('should report correct table/column names', function (done) { var modelBuilder = new ModelBuilder();