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

Commit f0a3c83c by Mick Hansen

fix(include): better support for include by association and where

1 parent a6b169e3
# 2.0.0-rc6
- [BUG] Fixed issue with including by association reference and where
# 2.0.0-rc5
- [BUG] Fixed issue with subquery creating `include.where` and a paranoid main model.#2749/#2769
- UniqueConstraintErrors will now extend from ValidationError making it possible to catch both with `.catch(ValidationError)`
......
......@@ -1952,10 +1952,10 @@ module.exports = (function() {
}
if (include.association && !include._pseudo && !include.model) {
if (include.association.associationType === 'BelongsTo') {
include.model = include.association.source;
} else {
if (include.association.source === this) {
include.model = include.association.target;
} else {
include.model = include.association.source;
}
}
......
......@@ -54,6 +54,30 @@ describe(Support.getTestDialectTeaser('Include'), function() {
});
});
it('should support a belongsTo association reference with a where', function () {
var Company = this.sequelize.define('Company', {name: DataTypes.STRING})
, User = this.sequelize.define('User', {})
, Employer = User.belongsTo(Company, {as: 'Employer', foreignKey: 'employerId'});
return this.sequelize.sync({force: true}).then(function() {
return Company.create({
name: 'CyberCorp'
}).then(function (company) {
return User.create({
employerId: company.get('id')
});
});
}).then(function () {
return User.findOne({
include: [
{association: Employer, where: {name: 'CyberCorp'}}
]
}).then(function (user) {
expect(user).to.be.ok;
});
});
});
it('should support a empty hasOne include', function(done) {
var Company = this.sequelize.define('Company', {})
, Person = this.sequelize.define('Person', {});
......@@ -134,6 +158,37 @@ describe(Support.getTestDialectTeaser('Include'), function() {
});
});
it('should support a hasMany association reference with a where condition', function () {
var User = this.sequelize.define('user', {})
, Task = this.sequelize.define('task', {title: DataTypes.STRING})
, Tasks = User.hasMany(Task);
Task.belongsTo(User);
return this.sequelize.sync({force: true}).then(function () {
return User.create().then(function (user) {
return Promise.join(
user.createTask({
title: 'trivial'
}),
user.createTask({
title: 'pursuit'
})
);
}).then(function () {
return User.find({
include: [
{association: Tasks, where: {title: 'trivial'}}
]
});
}).then(function (user) {
expect(user).to.be.ok;
expect(user.tasks).to.be.ok;
expect(user.tasks.length).to.equal(1);
});
});
});
it('should support a belongsToMany association reference', function () {
var User = this.sequelize.define('user', {})
, Group = this.sequelize.define('group', {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!