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

Commit 269d5c65 by John Giudici

Throw ForeignKeyConstraintError in mssql when trying to set an association that doesn't exist

1 parent a6f5a86e
......@@ -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(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./);
match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./) ||
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) {
return new sequelizeErrors.ForeignKeyConstraintError({
fields: null,
......
......@@ -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() {
var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING });
......
......@@ -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() {
var User = this.sequelize.define('UserXYZ', { username: 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!