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

Commit 65efbd3f by Harshith Kashyap Committed by Jan Aagaard Meier

generateThroughJoin Fix - V3 port (#6878)

1 parent 20907581
# Future
- [ADDED] UPSERT support for MSSQL
- [FIXED] Incorrect column name for generateThroughJoin
# 3.25.0
- [FIXED] Set `timestamps` and `paranoid` options from through model on `belongsToMany` association
......
......@@ -1307,7 +1307,7 @@ var QueryGenerator = {
var isBelongsTo = topInclude.association.associationType === 'BelongsTo';
var join = [
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(isBelongsTo ? topInclude.association.identifierField : topParent.model.primaryKeyAttributes[0]),
self.quoteIdentifier(topInclude.model.name) + '.' + self.quoteIdentifier(isBelongsTo ? topParent.model.primaryKeyAttributes[0] : topInclude.association.identifierField)
self.quoteIdentifier(topInclude.model.name) + '.' + self.quoteIdentifier(isBelongsTo ? topInclude.model.primaryKeyAttributes[0] : topInclude.association.identifierField)
].join(' = ');
$query = self.selectQuery(topInclude.model.tableName, {
attributes: [topInclude.model.primaryKeyAttributes[0]],
......
......@@ -1961,5 +1961,113 @@ describe(Support.getTestDialectTeaser('Include'), function() {
expect(parseInt(post["comments.commentCount"], 10)).to.equal(3);
});
});
it('Should return posts with nested include with inner join with a m:n association', function () {
var User = this.sequelize.define('User', {
username: {
type: DataTypes.STRING,
primaryKey: true
}
});
var Entity = this.sequelize.define('Entity', {
entity_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
creator: {
type: DataTypes.STRING,
allowNull: false
},
votes: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
}
});
var Post = this.sequelize.define('Post', {
post_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
}
});
var TaggableSentient = this.sequelize.define('TaggableSentient', {
nametag: {
type: DataTypes.STRING,
primaryKey: true
}
});
Entity.belongsTo(User, { foreignKey: 'creator', targetKey: 'username' });
Post.belongsTo(Entity, { foreignKey: 'post_id', targetKey: 'entity_id' });
Entity.belongsToMany(TaggableSentient, {
as: 'tags',
through: { model: 'EntityTag', unique: false },
foreignKey: 'entity_id',
otherKey: 'tag_name'
});
TaggableSentient.belongsToMany(Entity, {
as: 'tags',
through: { model: 'EntityTag', unique: false },
foreignKey: 'tag_name',
otherKey: 'entity_id'
});
return this.sequelize.sync({ force: true })
.then(function () {
return User.create({ username: 'bob' });
})
.then(function () {
return TaggableSentient.create({ nametag: 'bob' });
})
.then(function () {
return Entity.create({ creator: 'bob' });
})
.then(function(entity) {
return Promise.all([
Post.create({ post_id: entity.entity_id }),
entity.addTags('bob')
]);
})
.then(function() {
var options = {
include: [{
model: Entity,
required: true,
include: [{
model: User,
required: true
}, {
model: TaggableSentient,
as: 'tags',
required: true,
through: {
where: {
tag_name: ['bob']
}
}
}]
}],
limit: 5,
offset: 0
};
return Post.findAll(options);
})
.then(function(posts) {
expect(posts.length).to.equal(1);
expect(posts[0].Entity.creator).to.equal('bob');
expect(posts[0].Entity.tags.length).to.equal(1);
expect(posts[0].Entity.tags[0].EntityTag.tag_name).to.equal('bob');
expect(posts[0].Entity.tags[0].EntityTag.entity_id).to.equal(posts[0].post_id);
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!