Skip to content

Commit 89e13c1

Browse files
committed
Fixed generated SQL when using find with through.where and required true and the primary key attribute is different from the primary key field
1 parent 1c72bc9 commit 89e13c1

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

lib/dialects/abstract/query-generator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ var QueryGenerator = {
12661266

12671267
if (topInclude.through && Object(topInclude.through.model) === topInclude.through.model) {
12681268
$query = self.selectQuery(topInclude.through.model.getTableName(), {
1269-
attributes: [topInclude.through.model.primaryKeyAttributes[0]],
1269+
attributes: [topInclude.through.model.primaryKeyField],
12701270
include: Model.$validateIncludedElements({
12711271
model: topInclude.through.model,
12721272
include: [{
@@ -1277,7 +1277,7 @@ var QueryGenerator = {
12771277
model: topInclude.through.model,
12781278
where: { $and: [
12791279
self.sequelize.asIs([
1280-
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyAttributes[0]),
1280+
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyField),
12811281
self.quoteIdentifier(topInclude.through.model.name) + '.' + self.quoteIdentifier(topInclude.association.identifierField)
12821282
].join(' = ')),
12831283
topInclude.through.where

lib/dialects/mssql/query-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ var QueryGenerator = {
586586
if (options.limit || options.offset) {
587587
if (!options.order || (options.include && !subQueryOrder.length)) {
588588
fragment += (options.order && !isSubQuery) ? ', ' : ' ORDER BY ';
589-
fragment += this.quoteIdentifier(model.primaryKeyAttribute);
589+
fragment += this.quoteIdentifier(model.primaryKeyField);
590590
}
591591

592592
if (options.offset || options.limit) {

test/integration/include/find.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,45 @@ describe(Support.getTestDialectTeaser('Include'), function() {
134134
});
135135
});
136136

137+
it('should include a model with a through.where and required true clause when the PK field name and attribute name are different', function() {
138+
var A = this.sequelize.define('a', {})
139+
, B = this.sequelize.define('b', {})
140+
, AB = this.sequelize.define('a_b', {
141+
name: {
142+
type : DataTypes.STRING(40),
143+
field: 'name_id',
144+
primaryKey : true
145+
}
146+
});
147+
148+
A.belongsToMany(B, { through : AB });
149+
B.belongsToMany(A, { through : AB });
150+
151+
return this.sequelize
152+
.sync({force: true})
153+
.then(function() {
154+
return Promise.join(
155+
A.create({}),
156+
B.create({})
157+
);
158+
})
159+
.spread(function(a, b) {
160+
return a.addB(b, {name : 'Foobar'});
161+
})
162+
.then(function() {
163+
return A.find({
164+
include: [
165+
{model: B, through : { where: {name: 'Foobar'} }, required : true }
166+
]
167+
});
168+
})
169+
.then(function(a) {
170+
expect(a).to.not.equal(null);
171+
expect(a.get('bs')).to.have.length(1);
172+
});
173+
});
174+
175+
137176
it('should still pull the main record when an included model is not required and has where restrictions without matches', function() {
138177
var A = this.sequelize.define('a', {
139178
name: DataTypes.STRING(40)

0 commit comments

Comments
 (0)