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

Commit f232209b by Sushant Committed by Mick Hansen

Fix #6069, HasOne throws erro when try to update using a primary key (#6100)

* (test) hasOne fails on updating with primary key

* use Pk in hasOne injectSetter when possible
1 parent 5aba3f99
......@@ -5,6 +5,7 @@
- [FIXED] `Model.count` don't include attributes [#5057](https://github.com/sequelize/sequelize/issues/5057)
- [INTERNALS] Updated `inflection` dependency and pinned version and expose all used `inflection` methods on `Utils`
- [ADDED] `Sequelize.useInflection` method
- [FIXED] `hasOne` throws error on update with a primary key [#6069](https://github.com/sequelize/sequelize/issues/6069)
## BC breaks:
- Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive), previously was `()` (exclusive, exclusive)
......
......@@ -212,7 +212,7 @@ class HasOne extends Association {
return this[association.accessors.get](options).then(oldInstance => {
// TODO Use equals method once #5605 is resolved
alreadyAssociated = oldInstance && associatedInstance && _.every(association.target.primaryKeyAttributes, attribute =>
oldInstance.get(attribute, {raw: true}) === associatedInstance.get(attribute, {raw: true})
oldInstance.get(attribute, {raw: true}) === (associatedInstance.get ? associatedInstance.get(attribute, {raw: true}) : associatedInstance)
);
if (oldInstance && !alreadyAssociated) {
......
......@@ -239,6 +239,38 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
});
});
it('supports updating with a primary key instead of an object', function() {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING });
User.hasOne(Task);
return this.sequelize.sync({ force: true }).then(function() {
return Promise.all([
User.create({id: 1, username: 'foo'}),
Task.create({id: 20, title: 'bar'})
]);
})
.spread(function(user, task) {
return user.setTaskXYZ(task.id)
.then(() => user.getTaskXYZ())
.then((task) => {
expect(task).not.to.be.null;
return Promise.all([
user,
Task.create({id: 2, title: 'bar2'})
]);
});
})
.spread(function(user, task2) {
return user.setTaskXYZ(task2.id)
.then(() => user.getTaskXYZ())
.then((task) => {
expect(task).not.to.be.null;
});
});
});
it('supports setting same association twice', function () {
var Home = this.sequelize.define('home', {})
, User = this.sequelize.define('user');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!