diff --git a/lib/datasource.js b/lib/datasource.js index dbbd162f..8ca49a3f 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -515,7 +515,11 @@ DataSource.prototype.setupDataAccess = function(modelClass, settings) { idProp.type = idType; modelClass.definition.rawProperties[idName].type = idType; modelClass.definition.properties[idName].type = idType; - if (settings.forceId) { + var forceId = settings.forceId; + if (idProp.generated && forceId !== false) { + forceId = true; + } + if (forceId) { modelClass.validatesAbsenceOf(idName, { if: 'isNewRecord' }); } } diff --git a/test/crud-with-options.test.js b/test/crud-with-options.test.js index 235b30f2..870d8229 100644 --- a/test/crud-with-options.test.js +++ b/test/crud-with-options.test.js @@ -13,6 +13,7 @@ describe('crud-with-options', function() { before(function(done) { db = getSchema(); User = db.define('User', { + id: { type: Number, id: true }, seq: { type: Number, index: true }, name: { type: String, index: true, sort: true }, email: { type: String, index: true }, diff --git a/test/default-scope.test.js b/test/default-scope.test.js index d35c8690..ef902e3e 100644 --- a/test/default-scope.test.js +++ b/test/default-scope.test.js @@ -94,6 +94,7 @@ describe('default scope', function() { Person = db.define('Person', { name: String }, { scope: { include: 'things' }, + forceId: false, }); // inst is only valid for instance methods diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 277c760f..3897df43 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -766,7 +766,7 @@ describe('Models attached to a dataSource', function() { title: { type: String, length: 255, index: true }, content: { type: String }, comments: [String], - }); + }, { forceId: false }); }); beforeEach(function(done) { diff --git a/test/manipulation.test.js b/test/manipulation.test.js index b7ed34e6..6b9eb4e9 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -61,6 +61,21 @@ describe('manipulation', function() { Person.destroyAll(done); }); + describe('forceId', function() { + before(function(done) { + TestForceId = db.define('TestForceId'); + db.automigrate('TestForceId', done); + }); + + it('it defaults to forceId:true for generated id property', function(done) { + TestForceId.create({ id: 1 }, function(err, t) { + should.exist(err); + err.message.should.match(/can\'t be set/); + done(); + }); + }); + }); + it('should create instance', function(done) { Person.create({ name: 'Anatoliy' }, function(err, p) { p.name.should.equal('Anatoliy'); @@ -308,7 +323,7 @@ describe('manipulation', function() { it('should refuse to create object with duplicate id', function(done) { // NOTE(bajtos) We cannot reuse Person model here, // `settings.forceId` aborts the CREATE request at the validation step. - var Product = db.define('ProductTest', { name: String }); + var Product = db.define('ProductTest', { name: String }, { forceId: false }); db.automigrate('ProductTest', function(err) { if (err) return done(err); @@ -825,7 +840,7 @@ describe('manipulation', function() { title: { type: String, length: 255, index: true }, content: { type: String }, comments: [String], - }); + }, { forceId: false }); ds.automigrate('Post', done); }); @@ -1046,6 +1061,8 @@ describe('manipulation', function() { ds.automigrate('Post', done); }); beforeEach(function(done) { + // TODO(bajtos) add API to lib/observer - remove observers for all hooks + Post._observers = {}; Post.destroyAll(function() { Post.create({ title: 'a', content: 'AAA' }, function(err, p) { if (err) return done(err); diff --git a/test/relations.test.js b/test/relations.test.js index 0ad952d6..88ca46db 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -4190,7 +4190,7 @@ describe('relations', function() { tmp = getTransientDataSource(); // db = getSchema(); Person = db.define('Person', { name: String }); - Address = tmp.define('Address', { street: String }); + Address = tmp.define('Address', { street: String }, { forceId: false }); Address.validatesPresenceOf('street'); db.automigrate(['Person'], done); @@ -4488,7 +4488,7 @@ describe('relations', function() { // db = getSchema(); Category = db.define('Category', { name: String }); Job = db.define('Job', { name: String }); - Link = db.define('Link', { name: String, notes: String }); + Link = db.define('Link', { name: String, notes: String }, { forceId: false }); }); it('can be declared', function(done) {