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

Commit 4e54fdfb by Tom Jenkinson Committed by Mick Hansen

Fix so that belongsToMany association keys aren't deleted (#5871)

fix(belongs-to-many): constraint cascading are now properly preserved 
1 parent 1c0a66af
# Future
- [FIXED] Postgres DECIMAL precision. (PostgreSQL) [#4893](https://github.com/sequelize/sequelize/issues/4893)
- [FIXED] removeColumn tries to delete non-existant foreign key constraint (mysql) [#5808](https://github.com/sequelize/sequelize/issues/5808)
- [FIXED] Relation constraints not being applied correctly [#5865](https://github.com/sequelize/sequelize/issues/5865)
# 3.23.0
- [FIXED] Invalid query generated when using LIKE + ANY [#5736](https://github.com/sequelize/sequelize/issues/5736)
......
......@@ -311,9 +311,17 @@ BelongsToMany.prototype.injectAttributes = function() {
this.foreignIdentifier = this.otherKey;
// remove any PKs previously defined by sequelize
// but ignore any keys that are part of this association (#5865)
_.each(this.through.model.rawAttributes, function(attribute, attributeName) {
if (attribute.primaryKey === true && attribute._autoGenerated === true) {
delete self.through.model.rawAttributes[attributeName];
if (attributeName === self.foreignKey || attributeName === self.otherKey) {
// this key is still needed as it's part of the association
// so just set primaryKey to false
attribute.primaryKey = false;
}
else {
delete self.through.model.rawAttributes[attributeName];
}
self.primaryKeyDeleted = true;
}
});
......
......@@ -1373,7 +1373,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
this.User.belongsToMany(this.Task, { through: this.UserTasks });
this.Task.belongsToMany(this.User, { through: this.UserTasks });
expect(Object.keys(this.UserTasks.primaryKeys)).to.deep.equal(['TaskId', 'UserId']);
expect(Object.keys(this.UserTasks.primaryKeys).sort()).to.deep.equal(['TaskId', 'UserId']);
});
it('keeps the primary key if it was added by the user', function() {
......@@ -1426,7 +1426,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
this.User.belongsToMany(this.Task, { through: this.UsersTasks });
this.Task.belongsToMany(this.User, { through: this.UsersTasks });
expect(Object.keys(this.UsersTasks.primaryKeys)).to.deep.equal(['TaskId', 'UserId']);
expect(Object.keys(this.UsersTasks.primaryKeys).sort()).to.deep.equal(['TaskId', 'UserId']);
return Promise.all([
this.User.create({username: 'foo'}),
......
......@@ -502,4 +502,36 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
expect(Instance.prototype).not.to.have.property('addSupplementeds').which.is.a.function;
});
});
describe('constraints', function () {
it('work properly when through is a string', function() {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {});
User.belongsToMany(Group, { as: 'MyGroups', through: 'group_user', onUpdate: 'RESTRICT', onDelete: 'SET NULL' });
Group.belongsToMany(User, { as: 'MyUsers', through: 'group_user', onUpdate: 'SET NULL', onDelete: 'RESTRICT' });
expect(Group.associations.MyUsers.through.model === User.associations.MyGroups.through.model);
expect(Group.associations.MyUsers.through.model.rawAttributes.UserId.onUpdate).to.equal('RESTRICT');
expect(Group.associations.MyUsers.through.model.rawAttributes.UserId.onDelete).to.equal('SET NULL');
expect(Group.associations.MyUsers.through.model.rawAttributes.GroupId.onUpdate).to.equal('SET NULL');
expect(Group.associations.MyUsers.through.model.rawAttributes.GroupId.onDelete).to.equal('RESTRICT');
});
it('work properly when through is a model', function() {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {})
, UserGroup = this.sequelize.define('GroupUser', {}, {tableName: 'user_groups'});
User.belongsToMany(Group, { as: 'MyGroups', through: UserGroup, onUpdate: 'RESTRICT', onDelete: 'SET NULL' });
Group.belongsToMany(User, { as: 'MyUsers', through: UserGroup, onUpdate: 'SET NULL', onDelete: 'RESTRICT' });
expect(Group.associations.MyUsers.through.model === User.associations.MyGroups.through.model);
expect(Group.associations.MyUsers.through.model.rawAttributes.UserId.onUpdate).to.equal('RESTRICT');
expect(Group.associations.MyUsers.through.model.rawAttributes.UserId.onDelete).to.equal('SET NULL');
expect(Group.associations.MyUsers.through.model.rawAttributes.GroupId.onUpdate).to.equal('SET NULL');
expect(Group.associations.MyUsers.through.model.rawAttributes.GroupId.onDelete).to.equal('RESTRICT');
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!