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

Commit 7f5a251b by Mick Hansen

Merge pull request #4667 from Americas/master

Fixed generated SQL when using find with through.where and required true
2 parents 297328c6 89e13c1a
......@@ -1266,7 +1266,7 @@ var QueryGenerator = {
if (topInclude.through && Object(topInclude.through.model) === topInclude.through.model) {
$query = self.selectQuery(topInclude.through.model.getTableName(), {
attributes: [topInclude.through.model.primaryKeyAttributes[0]],
attributes: [topInclude.through.model.primaryKeyField],
include: Model.$validateIncludedElements({
model: topInclude.through.model,
include: [{
......@@ -1277,7 +1277,7 @@ var QueryGenerator = {
model: topInclude.through.model,
where: { $and: [
self.sequelize.asIs([
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyAttributes[0]),
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyField),
self.quoteIdentifier(topInclude.through.model.name) + '.' + self.quoteIdentifier(topInclude.association.identifierField)
].join(' = ')),
topInclude.through.where
......
......@@ -586,7 +586,7 @@ var QueryGenerator = {
if (options.limit || options.offset) {
if (!options.order || (options.include && !subQueryOrder.length)) {
fragment += (options.order && !isSubQuery) ? ', ' : ' ORDER BY ';
fragment += this.quoteIdentifier(model.primaryKeyAttribute);
fragment += this.quoteIdentifier(model.primaryKeyField);
}
if (options.offset || options.limit) {
......
......@@ -134,6 +134,45 @@ describe(Support.getTestDialectTeaser('Include'), function() {
});
});
it('should include a model with a through.where and required true clause when the PK field name and attribute name are different', function() {
var A = this.sequelize.define('a', {})
, B = this.sequelize.define('b', {})
, AB = this.sequelize.define('a_b', {
name: {
type : DataTypes.STRING(40),
field: 'name_id',
primaryKey : true
}
});
A.belongsToMany(B, { through : AB });
B.belongsToMany(A, { through : AB });
return this.sequelize
.sync({force: true})
.then(function() {
return Promise.join(
A.create({}),
B.create({})
);
})
.spread(function(a, b) {
return a.addB(b, {name : 'Foobar'});
})
.then(function() {
return A.find({
include: [
{model: B, through : { where: {name: 'Foobar'} }, required : true }
]
});
})
.then(function(a) {
expect(a).to.not.equal(null);
expect(a.get('bs')).to.have.length(1);
});
});
it('should still pull the main record when an included model is not required and has where restrictions without matches', function() {
var A = this.sequelize.define('a', {
name: DataTypes.STRING(40)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!