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

Commit de0fbff2 by Aleksandr Committed by Sushant

fix(model/create): doesnot work with custom sourceKey (#8284)

1 parent bde6d501
...@@ -3651,7 +3651,7 @@ class Model { ...@@ -3651,7 +3651,7 @@ class Model {
return include.association.throughModel.create(values, includeOptions); return include.association.throughModel.create(values, includeOptions);
}); });
} else { } else {
instance.set(include.association.foreignKey, this.get(this.constructor.primaryKeyAttribute, {raw: true})); instance.set(include.association.foreignKey, this.get(include.association.sourceKey || this.constructor.primaryKeyAttribute, {raw: true}));
return instance.save(includeOptions); return instance.save(includeOptions);
} }
}); });
......
...@@ -1305,6 +1305,33 @@ describe(Support.getTestDialectTeaser('HasMany'), () => { ...@@ -1305,6 +1305,33 @@ describe(Support.getTestDialectTeaser('HasMany'), () => {
}); });
}); });
it('should create with nested associated models', function() {
const User = this.User,
values = {
username: 'John',
email: 'john@example.com',
tasks: [{ title: 'Fix new PR' }]
};
return User.create(values, { include: ['tasks'] })
.then(user => {
// Make sure tasks are defined for created user
expect(user).to.have.property('tasks');
expect(user.tasks).to.be.an('array');
expect(user.tasks).to.lengthOf(1);
expect(user.tasks[0].title).to.be.equal(values.tasks[0].title, 'task title is correct');
return User.findOne({ where: { email: values.email } });
})
.then(user =>
user.getTasks()
.then(tasks => {
// Make sure tasks relationship is successful
expect(tasks).to.be.an('array');
expect(tasks).to.lengthOf(1);
expect(tasks[0].title).to.be.equal(values.tasks[0].title, 'task title is correct');
}));
});
}); });
describe('sourceKey with where clause in include', () => { describe('sourceKey with where clause in include', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!