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

You need to sign in or sign up before continuing.
Commit ce7865c0 by Mick Hansen

fix(includes): do not use subquery if required is false

1 parent ab741606
...@@ -789,7 +789,7 @@ module.exports = (function() { ...@@ -789,7 +789,7 @@ module.exports = (function() {
if (include.where) { if (include.where) {
targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions); targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions);
joinQueryItem += ' AND ' + targetWhere; joinQueryItem += ' AND ' + targetWhere;
if (subQuery) { if (subQuery && include.required) {
if (!options.where) options.where = {}; if (!options.where) options.where = {};
// Creating the as-is where for the subQuery, checks that the required association exists // Creating the as-is where for the subQuery, checks that the required association exists
......
...@@ -11,33 +11,29 @@ chai.config.includeStack = true ...@@ -11,33 +11,29 @@ chai.config.includeStack = true
describe(Support.getTestDialectTeaser("Include"), function () { describe(Support.getTestDialectTeaser("Include"), function () {
describe('find', function () { describe('find', function () {
it('should include a non required model, with conditions and two includes N:M 1:M', function ( done ) {
it( 'Try to include a non required model, with conditions and two includes N:M 1:M', function ( done ) { var A = this.sequelize.define('A', { name: DataTypes.STRING(40) }, { paranoid: true })
var DT = DataTypes, , B = this.sequelize.define('B', { name: DataTypes.STRING(40) }, { paranoid: true })
S = this.sequelize, , C = this.sequelize.define('C', { name: DataTypes.STRING(40) }, { paranoid: true })
A = S.define('A', { name: DT.STRING(40) }, { paranoid: true }), , D = this.sequelize.define('D', { name: DataTypes.STRING(40) }, { paranoid: true });
B = S.define('B', { name: DT.STRING(40) }, { paranoid: true }),
C = S.define('C', { name: DT.STRING(40) }, { paranoid: true }),
D = S.define('D', { name: DT.STRING(40) }, { paranoid: true })
// Associations // Associations
A.hasMany( B ) A.hasMany(B);
B.belongsTo( B ) B.belongsTo(B);
B.belongsTo( D ) B.belongsTo(D);
B.hasMany( C, { B.hasMany(C, {
through: 'BC', through: 'BC',
}) });
C C.hasMany(B, {
.hasMany( B, {
through: 'BC', through: 'BC',
}) });
D D.hasMany(B);
.hasMany( B )
S.sync({ force: true }).done( function ( err ) { expect( err ).not.to.be.ok this.sequelize.sync({ force: true }).done(function ( err ) {
expect( err ).not.to.be.ok;
A.find({ A.find({
include: [ include: [
...@@ -47,24 +43,25 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -47,24 +43,25 @@ describe(Support.getTestDialectTeaser("Include"), function () {
]} ]}
] ]
}).done( function ( err ) { }).done( function ( err ) {
expect( err ).not.to.be.ok expect( err ).not.to.be.ok;
done() done();
}) });
});
})
}); });
it("should still pull the main record when an included model is not required and has where restrictions without matches", function () { it("should still pull the main record when an included model is not required and has where restrictions without matches", function () {
var DT = DataTypes, var A = this.sequelize.define('A', {
S = this.sequelize, name: DataTypes.STRING(40)
A = S.define( 'A', {name: DT.STRING(40)} ), })
B = S.define( 'B', {name: DT.STRING(40)} ); , B = this.sequelize.define('B', {
name: DataTypes.STRING(40)
});
A.hasMany(B); A.hasMany(B);
B.hasMany(A); B.hasMany(A);
return S return this.sequelize
.sync({force: true}) .sync({force: true})
.then(function () { .then(function () {
return A.create({ return A.create({
...@@ -75,7 +72,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -75,7 +72,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
return A.find({ return A.find({
where: {name: 'Foobar'}, where: {name: 'Foobar'},
include: [ include: [
{model: B, where: {name: 'idontexist'}, require: false} {model: B, where: {name: 'idontexist'}, required: false}
] ]
}); });
}) })
...@@ -84,7 +81,5 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -84,7 +81,5 @@ describe(Support.getTestDialectTeaser("Include"), function () {
expect(a.get('bs')).to.deep.equal([]); expect(a.get('bs')).to.deep.equal([]);
}); });
}); });
}); });
});
}) \ No newline at end of file
\ No newline at end of file
...@@ -1681,15 +1681,14 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1681,15 +1681,14 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
it("should still pull the main record(s) when an included model is not required and has where restrictions without matches", function () { it("should still pull the main record(s) when an included model is not required and has where restrictions without matches", function () {
var DT = DataTypes, var self = this
S = this.sequelize, , A = this.sequelize.define( 'A', {name: DataTypes.STRING(40)} )
A = S.define( 'A', {name: DT.STRING(40)} ), , B = this.sequelize.define( 'B', {name: DataTypes.STRING(40)} );
B = S.define( 'B', {name: DT.STRING(40)} );
A.hasMany(B); A.hasMany(B);
B.hasMany(A); B.hasMany(A);
return S return this.sequelize
.sync({force: true}) .sync({force: true})
.then(function () { .then(function () {
return A.create({ return A.create({
...@@ -1700,7 +1699,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1700,7 +1699,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
return A.findAll({ return A.findAll({
where: {name: 'Foobar'}, where: {name: 'Foobar'},
include: [ include: [
{model: B, where: {name: 'idontexist'}, require: false} {model: B, where: {name: 'idontexist'}, required: false}
] ]
}); });
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!