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

Commit 1fe24016 by warkenji Committed by Sushant

fix(sequelize.col): associations (#11419)

1 parent 3834fe20
...@@ -898,7 +898,7 @@ class QueryGenerator { ...@@ -898,7 +898,7 @@ class QueryGenerator {
if (identifiers.includes('.')) { if (identifiers.includes('.')) {
identifiers = identifiers.split('.'); identifiers = identifiers.split('.');
const head = identifiers.slice(0, identifiers.length - 1).join('.'); const head = identifiers.slice(0, identifiers.length - 1).join('->');
const tail = identifiers[identifiers.length - 1]; const tail = identifiers[identifiers.length - 1];
return `${this.quoteIdentifier(head)}.${this.quoteIdentifier(tail)}`; return `${this.quoteIdentifier(head)}.${this.quoteIdentifier(tail)}`;
......
...@@ -48,6 +48,58 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -48,6 +48,58 @@ describe(Support.getTestDialectTeaser('Include'), () => {
}); });
}); });
it('should support to use associations with Sequelize.col', function() {
const Table1 = this.sequelize.define('Table1');
const Table2 = this.sequelize.define('Table2');
const Table3 = this.sequelize.define('Table3', { value: DataTypes.INTEGER });
Table1.hasOne(Table2, { foreignKey: 'Table1Id' });
Table2.hasMany(Table3, { as: 'Tables3', foreignKey: 'Table2Id' });
return this.sequelize.sync({ force: true }).then(() => {
return Table1.create().then(table1 => {
return Table2.create({
Table1Id: table1.get('id')
});
}).then(table2 => {
return Table3.bulkCreate([
{
Table2Id: table2.get('id'),
value: 5
},
{
Table2Id: table2.get('id'),
value: 7
}
], {
validate: true
});
});
}).then(() => {
return Table1.findAll({
raw: true,
attributes: [
[Sequelize.fn('SUM', Sequelize.col('Table2.Tables3.value')), 'sum']
],
include: [
{
model: Table2,
attributes: [],
include: [
{
model: Table3,
as: 'Tables3',
attributes: []
}
]
}
]
}).then(result => {
expect(result.length).to.equal(1);
expect(parseInt(result[0].sum, 10)).to.eq(12);
});
});
});
it('should support a belongsTo association reference with a where', function() { it('should support a belongsTo association reference with a where', function() {
const Company = this.sequelize.define('Company', { name: DataTypes.STRING }), const Company = this.sequelize.define('Company', { name: DataTypes.STRING }),
User = this.sequelize.define('User', {}), User = this.sequelize.define('User', {}),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!