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

Commit 7b80ade6 by Jan Aagaard Meier

Support calling setAssociation twice on hasOne. Closes #5315

1 parent 8ab2dd32
# Future # Future
- [FIXED] Confirmed that values modified in validation hooks are preserved [#3534](https://github.com/sequelize/sequelize/issues/3534) - [FIXED] Confirmed that values modified in validation hooks are preserved [#3534](https://github.com/sequelize/sequelize/issues/3534)
- [FIXED] Support lower case type names in SQLite [#5482](https://github.com/sequelize/sequelize/issues/5482) - [FIXED] Support lower case type names in SQLite [#5482](https://github.com/sequelize/sequelize/issues/5482)
- [FIXED] Support calling `setAssociation` twice on `hasOne` [#5315](https://github.com/sequelize/sequelize/issues/5315)
- [INTERNALS] Removed dependency on wellknown in favor of terraformer-wkt-parser - [INTERNALS] Removed dependency on wellknown in favor of terraformer-wkt-parser
- [ADDED] Benchmarking feature [#2494](https://github.com/sequelize/sequelize/issues/2494) - [ADDED] Benchmarking feature [#2494](https://github.com/sequelize/sequelize/issues/2494)
......
...@@ -173,12 +173,18 @@ HasOne.prototype.injectSetter = function(instancePrototype) { ...@@ -173,12 +173,18 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
var association = this; var association = this;
instancePrototype[this.accessors.set] = function(associatedInstance, options) { instancePrototype[this.accessors.set] = function(associatedInstance, options) {
var instance = this; var instance = this,
alreadyAssociated;
options = options || {}; options = options || {};
options.scope = false; options.scope = false;
return instance[association.accessors.get](options).then(function(oldInstance) { return instance[association.accessors.get](options).then(function(oldInstance) {
if (oldInstance) { // TODO Use equals method once #5605 is resolved
alreadyAssociated = oldInstance && associatedInstance && _.every(association.target.primaryKeyAttributes, function(attribute) {
return oldInstance.get(attribute, {raw: true}) === associatedInstance.get(attribute, {raw: true});
});
if (oldInstance && !alreadyAssociated) {
oldInstance[association.foreignKey] = null; oldInstance[association.foreignKey] = null;
return oldInstance.save(_.extend({}, options, { return oldInstance.save(_.extend({}, options, {
fields: [association.foreignKey], fields: [association.foreignKey],
...@@ -187,7 +193,7 @@ HasOne.prototype.injectSetter = function(instancePrototype) { ...@@ -187,7 +193,7 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
})); }));
} }
}).then(function() { }).then(function() {
if (associatedInstance) { if (associatedInstance && !alreadyAssociated) {
if (!(associatedInstance instanceof association.target.Instance)) { if (!(associatedInstance instanceof association.target.Instance)) {
var tmpInstance = {}; var tmpInstance = {};
tmpInstance[association.target.primaryKeyAttribute] = associatedInstance; tmpInstance[association.target.primaryKeyAttribute] = associatedInstance;
......
...@@ -334,6 +334,28 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -334,6 +334,28 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
}); });
it('supports setting same association twice', function () {
var Home = this.sequelize.define('home', {})
, User = this.sequelize.define('user');
Home.belongsTo(User);
return this.sequelize.sync({ force: true }).bind({}).then(function () {
return Promise.all([
Home.create(),
User.create()
]);
}).spread(function (home, user) {
this.home = home;
this.user = user;
return home.setUser(user);
}).then(function() {
return this.home.setUser(this.user);
}).then(function () {
return expect(this.home.getUser()).to.eventually.have.property('id', this.user.get('id'));
});
});
}); });
describe('createAssociation', function() { describe('createAssociation', function() {
......
...@@ -217,6 +217,28 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -217,6 +217,28 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
}); });
}); });
}); });
it('supports setting same association twice', function () {
var Home = this.sequelize.define('home', {})
, User = this.sequelize.define('user');
User.hasOne(Home);
return this.sequelize.sync({ force: true }).bind({}).then(function () {
return Promise.all([
Home.create(),
User.create()
]);
}).spread(function (home, user) {
this.home = home;
this.user = user;
return user.setHome(home);
}).then(function() {
return this.user.setHome(this.home);
}).then(function () {
return expect(this.user.getHome()).to.eventually.have.property('id', this.home.get('id'));
});
});
}); });
describe('createAssociation', function() { describe('createAssociation', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!