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

Commit 34abeaa0 by Sushant Committed by Mick Hansen

check for parent before preparing its include attribute map (#6297)

1 parent df3cb0c2
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
- [FIXED] `Model.count` with `options.col` and `options.include` works properly now - [FIXED] `Model.count` with `options.col` and `options.include` works properly now
- [FIXED] `bulkCreate` don't map fields to attributes properly [#4476](https://github.com/sequelize/sequelize/issues/4476)[#3908](https://github.com/sequelize/sequelize/issues/3908)[#4103](https://github.com/sequelize/sequelize/issues/4103)[#3764](https://github.com/sequelize/sequelize/issues/3764)[#3789](https://github.com/sequelize/sequelize/issues/3789)[#4600](https://github.com/sequelize/sequelize/issues/4600) - [FIXED] `bulkCreate` don't map fields to attributes properly [#4476](https://github.com/sequelize/sequelize/issues/4476)[#3908](https://github.com/sequelize/sequelize/issues/3908)[#4103](https://github.com/sequelize/sequelize/issues/4103)[#3764](https://github.com/sequelize/sequelize/issues/3764)[#3789](https://github.com/sequelize/sequelize/issues/3789)[#4600](https://github.com/sequelize/sequelize/issues/4600)
- [FIXED] `sync` don't handle global `options.logging` properly [#5788](https://github.com/sequelize/sequelize/issues/5788) - [FIXED] `sync` don't handle global `options.logging` properly [#5788](https://github.com/sequelize/sequelize/issues/5788)
- [FIXED] `attribute:[]` throw errors with `include` or `through` [#5078](https://github.com/sequelize/sequelize/issues/5078) [#4222](https://github.com/sequelize/sequelize/issues/4222) [#5958](https://github.com/sequelize/sequelize/issues/5958) [#5590](https://github.com/sequelize/sequelize/issues/5590) [#6139](https://github.com/sequelize/sequelize/issues/6139) [#4866](https://github.com/sequelize/sequelize/issues/4866) [#6242](https://github.com/sequelize/sequelize/issues/6242)
## BC breaks: ## 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) - 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)
......
...@@ -580,7 +580,9 @@ class AbstractQuery { ...@@ -580,7 +580,9 @@ class AbstractQuery {
$lastKeyPrefix = lastKeyPrefix(prevKey); $lastKeyPrefix = lastKeyPrefix(prevKey);
if (includeMap[prevKey].association.isSingleAssociation) { if (includeMap[prevKey].association.isSingleAssociation) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values; if ($parent) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values;
}
} else { } else {
if (!$parent[$lastKeyPrefix]) { if (!$parent[$lastKeyPrefix]) {
$parent[$lastKeyPrefix] = []; $parent[$lastKeyPrefix] = [];
...@@ -667,7 +669,9 @@ class AbstractQuery { ...@@ -667,7 +669,9 @@ class AbstractQuery {
$lastKeyPrefix = lastKeyPrefix(prevKey); $lastKeyPrefix = lastKeyPrefix(prevKey);
if (includeMap[prevKey].association.isSingleAssociation) { if (includeMap[prevKey].association.isSingleAssociation) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values; if ($parent) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values;
}
} else { } else {
if (!$parent[$lastKeyPrefix]) { if (!$parent[$lastKeyPrefix]) {
$parent[$lastKeyPrefix] = []; $parent[$lastKeyPrefix] = [];
......
...@@ -859,6 +859,98 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -859,6 +859,98 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
}); });
describe('properly handles attributes:[] cases', function () {
beforeEach(function () {
this.Animal = this.sequelize.define('Animal', {
name: Sequelize.STRING,
age: Sequelize.INTEGER
});
this.Kingdom = this.sequelize.define('Kingdom', {
name: Sequelize.STRING
});
this.AnimalKingdom = this.sequelize.define('AnimalKingdom', {
relation: Sequelize.STRING,
mutation: Sequelize.BOOLEAN
});
this.Kingdom.belongsToMany(this.Animal, { through: this.AnimalKingdom });
return this.sequelize.sync({ force: true })
.then(() => Sequelize.Promise.all([
this.Animal.create({ name: 'Dog', age: 20 }),
this.Animal.create({ name: 'Cat', age: 30 }),
this.Animal.create({ name: 'Peacock', age: 25 }),
this.Animal.create({ name: 'Fish', age: 100 })
]))
.spread((a1, a2, a3, a4) => Sequelize.Promise.all([
this.Kingdom.create({ name: 'Earth' }),
this.Kingdom.create({ name: 'Water' }),
this.Kingdom.create({ name: 'Wind' })
]).spread((k1, k2, k3) => (
Sequelize.Promise.all([
k1.addAnimals([a1, a2]),
k2.addAnimals([a4]),
k3.addAnimals([a3])
])
)));
});
it('N:M with ignoring include.attributes only', function () {
return this.Kingdom.findAll({
include:[{
model: this.Animal,
where: { age: { $gte : 29 } },
attributes: []
}]
}).then((kingdoms) => {
expect(kingdoms.length).to.be.eql(2);
kingdoms.forEach((kingdom) => {
// include.attributes:[] , model doesn't exists
expect(kingdom.Animals).to.not.exist;
});
});
});
it('N:M with ignoring through.attributes only', function () {
return this.Kingdom.findAll({
include:[{
model: this.Animal,
where: { age: { $gte : 29 } },
through: {
attributes: []
}
}]
}).then((kingdoms) => {
expect(kingdoms.length).to.be.eql(2);
kingdoms.forEach((kingdom) => {
expect(kingdom.Animals).to.exist; // include model exists
expect(kingdom.Animals[0].AnimalKingdom).to.not.exist; // through doesn't exists
});
});
});
it('N:M with ignoring include.attributes but having through.attributes', function () {
return this.Kingdom.findAll({
include:[{
model: this.Animal,
where: { age: { $gte : 29 } },
attributes: [],
through: {
attributes: ['mutation']
}
}]
}).then((kingdoms) => {
expect(kingdoms.length).to.be.eql(2);
kingdoms.forEach((kingdom) => {
// include.attributes: [], model doesn't exists
expect(kingdom.Animals).to.not.exist;
});
});
});
});
}); });
describe('order by eager loaded tables', function() { describe('order by eager loaded tables', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!