不要怂,就是干,撸起袖子干!

Commit 17f8de82 by Mick Hansen

Merge pull request #3986 from johngiudici/mssql-foreign-key-error

Throw ForeignKeyConstraintError in mssql when trying to set an association that doesn't exist
2 parents a5f9c420 269d5c65
...@@ -200,8 +200,10 @@ Query.prototype.formatError = function (err) { ...@@ -200,8 +200,10 @@ Query.prototype.formatError = function (err) {
}); });
} }
match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./); match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./) ||
match = err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./); err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./) ||
err.message.match(/The INSERT statement conflicted with the FOREIGN KEY constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./) ||
err.message.match(/The UPDATE statement conflicted with the FOREIGN KEY constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./);
if (match && match.length > 0) { if (match && match.length > 0) {
return new sequelizeErrors.ForeignKeyConstraintError({ return new sequelizeErrors.ForeignKeyConstraintError({
fields: null, fields: null,
......
...@@ -225,6 +225,21 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -225,6 +225,21 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
it('should throw a ForeignKeyConstraintError if the associated record does not exist', function() {
var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING });
Task.belongsTo(User);
return this.sequelize.sync({ force: true }).then(function() {
return expect(Task.create({ title: 'task', UserXYZId: 5 })).to.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () {
return Task.create({ title: 'task' }).then(function(task) {
return expect(Task.update({ title: 'taskUpdate', UserXYZId: 5 }, { where: { id: task.id } })).to.be.rejectedWith(Sequelize.ForeignKeyConstraintError);
});
});
});
});
it('supports passing the primary key instead of an object', function() { it('supports passing the primary key instead of an object', function() {
var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING }) var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING }); , Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING });
......
...@@ -182,6 +182,23 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -182,6 +182,23 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
}); });
}); });
it('should throw a ForeignKeyConstraintError if the associated record does not exist', function() {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING });
User.hasOne(Task);
return User.sync({ force: true }).then(function() {
return Task.sync({ force: true }).then(function() {
return expect(Task.create({ title: 'task', UserXYZId: 5 })).to.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () {
return Task.create({ title: 'task' }).then(function(task) {
return expect(Task.update({ title: 'taskUpdate', UserXYZId: 5 }, { where: { id: task.id } })).to.be.rejectedWith(Sequelize.ForeignKeyConstraintError);
});
});
});
});
});
it('supports passing the primary key instead of an object', function() { it('supports passing the primary key instead of an object', function() {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING }) var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING }); , Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!