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

Commit be342a46 by GongYi Committed by Sushant

Let nested include query returns results when quoteIdentifiers is false on Postgres (#6375)

* Keep the key sequence when translate the lower case key name to camelcase name of each row
* Use arrow functions to minimize or eliminate self
* Use arrow functions to minimize or eliminate self in test
* Refine the format of 'else'
1 parent f4f0edc7
......@@ -35,6 +35,7 @@
- [FIXED] Generate correct SQL of nested include when quoteIdentifiers is false. (Postgres) [#6351](https://github.com/sequelize/sequelize/issues/6351)
- [FIXED] Generate correct SQL for JSON attributes with quote.
[#6406](https://github.com/sequelize/sequelize/issues/6406)
- [FIXED] Nested query return correct result when quoteIdentifiers is false. (Postgres) [#6363](https://github.com/sequelize/sequelize/issues/6363)
## BC breaks:
- Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive), previously was `()` (exclusive, exclusive)
......
......@@ -194,17 +194,17 @@ class Query extends AbstractQuery {
m[k.toLowerCase()] = k;
return m;
}, {});
for (const row of rows) {
for (const key of Object.keys(row)) {
rows = _.map(rows,(row)=> {
return _.mapKeys(row, (value, key)=> {
const targetAttr = attrsMap[key];
if (typeof targetAttr === 'string' && targetAttr !== key) {
row[targetAttr] = row[key];
delete row[key];
return targetAttr;
} else {
return key;
}
}
}
});
});
}
return this.handleSelectQuery(rows);
} else if (QueryTypes.DESCRIBE === this.options.type) {
result = {};
......
......@@ -5,6 +5,8 @@
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../../support')
, Sequelize = Support.Sequelize
, Promise = Sequelize.Promise
, dialect = Support.getTestDialect()
, DataTypes = require(__dirname + '/../../../../lib/data-types')
, sequelize = require(__dirname + '/../../../../lib/sequelize');
......@@ -957,6 +959,143 @@ if (dialect.match(/^postgres/)) {
});
});
});
it('can select nested include', function() {
this.sequelize.options.quoteIdentifiers = false;
this.sequelize.getQueryInterface().QueryGenerator.options.quoteIdentifiers = false;
this.Professor = this.sequelize.define('Professor', {
fullName: DataTypes.STRING
}, {
quoteIdentifiers: false
});
this.Class = this.sequelize.define('Class', {
name: DataTypes.STRING
}, {
quoteIdentifiers: false
});
this.Student = this.sequelize.define('Student', {
fullName: DataTypes.STRING
}, {
quoteIdentifiers: false
});
this.ClassStudent = this.sequelize.define('ClassStudent', {
}, {
quoteIdentifiers: false,
tableName: 'class_student'
});
this.Professor.hasMany(this.Class);
this.Class.belongsTo(this.Professor);
this.Class.belongsToMany(this.Student,{through: this.ClassStudent});
this.Student.belongsToMany(this.Class,{through: this.ClassStudent});
return this.Professor.sync({ force: true })
.then(()=> {
return this.Student.sync({ force: true });
})
.then(()=> {
return this.Class.sync({ force: true });
})
.then(()=> {
return this.ClassStudent.sync({ force: true });
})
.then(()=> {
return this.Professor.bulkCreate([
{
id: 1,
fullName: 'Albus Dumbledore'
},
{
id: 2,
fullName: 'Severus Snape'
}
]);
})
.then(()=> {
return this.Class.bulkCreate([
{
id: 1,
name: 'Transfiguration',
ProfessorId: 1
},
{
id: 2,
name: 'Potions',
ProfessorId: 2
},
{
id: 3,
name: 'Defence Against the Dark Arts',
ProfessorId: 2
}
]);
})
.then(()=> {
return this.Student.bulkCreate([
{
id: 1,
fullName: 'Harry Potter',
},
{
id: 2,
fullName: 'Ron Weasley',
},
{
id: 3,
fullName: 'Ginny Weasley',
},
{
id: 4,
fullName: 'Hermione Granger',
}
]);
})
.then(()=> {
return Promise.all([
this.Student.findById(1)
.then((Harry)=> {
return Harry.setClasses([1,2,3]);
}),
this.Student.findById(2)
.then((Ron)=> {
return Ron.setClasses([1,2]);
}),
this.Student.findById(3)
.then((Ginny)=> {
return Ginny.setClasses([2,3]);
}),
this.Student.findById(4)
.then((Hermione)=> {
return Hermione.setClasses([1,2,3]);
})
]);
})
.then(()=> {
return this.Professor.findAll({
include:[
{
model: this.Class,
include: [
{
model: this.Student
}
]
}
],
order: [
['id'],
[this.Class, 'id'],
[this.Class, this.Student, 'id']
]
});
})
.then((professors)=> {
expect(professors.length).to.eql(2);
expect(professors[0].fullName).to.eql('Albus Dumbledore');
expect(professors[0].Classes.length).to.eql(1);
expect(professors[0].Classes[0].Students.length).to.eql(3);
})
.finally(()=> {
this.sequelize.getQueryInterface().QueryGenerator.options.quoteIdentifiers = true;
});
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!