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

Commit 78c3f91f by Jan Aagaard Meier

Merge pull request #4354 from glutentag/fix4353

Fix instance reload doesn't set to null a destroyed include

Conflicts:
	changelog.md
	lib/instance.js
2 parents d2177421 5a1ded70
# Next # Next
- [ADDED] beforeRestore/afterRestore hooks [#4371](https://github.com/sequelize/sequelize/issues/4371) - [ADDED] beforeRestore/afterRestore hooks [#4371](https://github.com/sequelize/sequelize/issues/4371)
- [INTERNALS] `options` has been renamed to `$options` in instance.js [#4429](https://github.com/sequelize/sequelize/pull/4429) - [INTERNALS] `options` has been renamed to `$options` in instance.js [#4429](https://github.com/sequelize/sequelize/pull/4429)
- [FIXED] Reload doesn't synchronize a null include [#4353](https://github.com/sequelize/sequelize/issues/4353)
# 3.8.0 # 3.8.0
- [ADDED] `version` on `Sequelize` returning the current npm/package.json version [#4459](https://github.com/sequelize/sequelize/pull/4459) - [ADDED] `version` on `Sequelize` returning the current npm/package.json version [#4459](https://github.com/sequelize/sequelize/pull/4459)
......
...@@ -312,7 +312,7 @@ Instance.prototype.set = function(key, value, options) { // testhint options:non ...@@ -312,7 +312,7 @@ Instance.prototype.set = function(key, value, options) { // testhint options:non
} else { } else {
// Check if we have included models, and if this key matches the include model names/aliases // Check if we have included models, and if this key matches the include model names/aliases
if (this.$options && this.$options.include && this.$options.includeNames.indexOf(key) !== -1 && value) { if (this.$options && this.$options.include && this.$options.includeNames.indexOf(key) !== -1) {
// Pass it on to the include handler // Pass it on to the include handler
this._setInclude(key, value, options); this._setInclude(key, value, options);
return; return;
...@@ -454,7 +454,7 @@ Instance.prototype._setInclude = function(key, value, options) { ...@@ -454,7 +454,7 @@ Instance.prototype._setInclude = function(key, value, options) {
value = value[0]; value = value[0];
} }
isEmpty = value && value[primaryKeyAttribute] === null; isEmpty = (value && value[primaryKeyAttribute] === null) || (value === null);
self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions); self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions);
} else { } else {
isEmpty = value[0] && value[0][primaryKeyAttribute] === null; isEmpty = value[0] && value[0][primaryKeyAttribute] === null;
......
...@@ -512,6 +512,100 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -512,6 +512,100 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
}); });
}); });
it('should set an association to null after deletion, 1-1', function() {
var Shoe = this.sequelize.define('Shoe', { brand: DataTypes.STRING })
, Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Player.hasOne(Shoe);
Shoe.belongsTo(Player);
return this.sequelize.sync({force: true}).then(function() {
return Shoe.create({
brand: 'the brand',
Player: {
name: 'the player'
}
}, {include: [Player]});
}).then(function(shoe) {
return Player.findOne({
where: { id: shoe.Player.id },
include: [Shoe]
}).then(function(lePlayer) {
expect(lePlayer.Shoe).not.to.be.null;
return lePlayer.Shoe.destroy().return(lePlayer);
}).then(function(lePlayer) {
return lePlayer.reload();
}).then(function(lePlayer) {
expect(lePlayer.Shoe).to.be.null;
});
});
});
it('should set an association to empty after all deletion, 1-N', function() {
var Team = this.sequelize.define('Team', { name: DataTypes.STRING })
, Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Team.hasMany(Player);
Player.belongsTo(Team);
return this.sequelize.sync({force: true}).then(function() {
return Team.create({
name: 'the team',
Players: [{
name: 'the player1'
}, {
name: 'the player2'
}]
}, {include: [Player]});
}).then(function(team) {
return Team.findOne({
where: { id: team.id },
include: [Player]
}).then(function(leTeam) {
expect(leTeam.Players).not.to.be.empty;
return leTeam.Players[1].destroy().then(function() {
return leTeam.Players[0].destroy();
}).return(leTeam);
}).then(function(leTeam) {
return leTeam.reload();
}).then(function(leTeam) {
expect(leTeam.Players).to.be.empty;
});
});
});
it('should update the associations after one element deleted', function() {
var Team = this.sequelize.define('Team', { name: DataTypes.STRING })
, Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Team.hasMany(Player);
Player.belongsTo(Team);
return this.sequelize.sync({force: true}).then(function() {
return Team.create({
name: 'the team',
Players: [{
name: 'the player1'
}, {
name: 'the player2'
}]
}, {include: [Player]});
}).then(function(team) {
return Team.findOne({
where: { id: team.id },
include: [Player]
}).then(function(leTeam) {
expect(leTeam.Players).to.have.length(2);
return leTeam.Players[0].destroy().return(leTeam);
}).then(function(leTeam) {
return leTeam.reload();
}).then(function(leTeam) {
expect(leTeam.Players).to.have.length(1);
});
});
});
}); });
describe('default values', function() { describe('default values', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!