Repository navigation
Description
What you are doing?
I'm trying to execute a query on an entity with a many to many relationship, including the related model and using attributes.
// entityA definition
module.exports = function (sequelize, DataTypes, sanitize) {
...
var EntityA = sequelize.define('entityA', {
// SOME PROPERTIES
...
},
{
paranoid: true,
classMethods: {
associate: function (models) {
...
EntityA.belongsToMany(models.entityB, {through: models.entityA_entityB});
...
}
},
...
});
return EntityA;
};
// entityB definition
module.exports = function (sequelize, DataTypes) {
...
var EntityB = sequelize.define('entityB', {
// SOME PROPERTIES
...
},
{
paranoid: true,
classMethods: {
associate: function (models) {
...
EntityB.belongsToMany(models.entityA, {through: models.entityA_entityB});
...
}
},
...
});
return EntityB;
};
// entityA_entityB definition
module.exports = function(sequelize, DataTypes) {
return sequelize.define('entityA_entityB', {
// SOME PROPERTIES
...
},
{
paranoid: true
});
};
// Querying the entities
models.entityA.findAll({
where: {
id: {
$ne: SOME_VALUE
}
},
include: [{
model: models.entityB,
where: {
id: req.query.entityBId
},
attributes: ['id'], // MUST BE SPECIFIED FOR THE QUERY TO BE EXECUTED SUCCESSFULLY
through: {
where: {
status: {
$ne: 'SOME_VALUE'
}
},
attributes: []
}
},
{
// OTHER INCLUSIONS
...
}],
attributes: ['id', 'some_other_attribute']
}).then(function (entitiesA) {
return callback(null, entitiesA);
}, function (err) {
return callback(err);
});Test case that reproduces the error
var Sequelize = require('sequelize');
var sequelize = new Sequelize('DB_NAME, 'DB_USERNAME', 'DB_PASSWORD', {
host: "DB_HOST",
dialect: "mysql",
logging: false,
charset: "utf8",
collate: "utf8_general_ci",
pool: {
maxIdleTime: 60000,
minConnections: 0
}
});
var EntityAEntityB = sequelize.define('EntityAEntityB', { type: Sequelize.STRING });
var EntityA = sequelize.define('EntityA',
{
name: Sequelize.STRING
});
var EntityB = sequelize.define('EntityB',
{
name: Sequelize.STRING
});
EntityA.belongsToMany(EntityB, {through: 'EntityAEntityB'});
EntityB.belongsToMany(EntityA, {through: 'EntityAEntityB'});
EntityA.sync()
.then(function () { return EntityB.sync(); })
.then(function () { return EntityAEntityB.sync(); })
.then(function () {
return EntityB.create({ name: 'My Entity B' });
})
.then(function (entityB) {
return EntityA.create({ name: 'My Entity A' })
.then(function (entityA) {
return entityA.addEntityBs(entityB, { type: 'My type' });
});
})
.then(function () {
console.log('Running');
EntityA.findAll({
include: [{
model: EntityB,
attributes: [],
// attributes: ['id'], // uncomment this and it will work!
through: {
attributes: []
}
}]
}).then(function (entitiesA) {
console.log('Entities A -> ', entitiesA);
process.exit(0);
});
});What do you expect to happen?
I expect the query to be executed without having to specify the attribute 'id' in the include.attribute array.
What is actually happening?
The findAll query fails with the following message:
Uncaught TypeError: Cannot set property 'entityA_entityB' of undefined
at groupJoinData (node_modules/sequelize/lib/dialects/abstract/query.js:276:41)
at AbstractQuery.handleSelectQuery (node_modules/sequelize/lib/dialects/abstract/query.js:583:15)
at Query.formatResults (node_modules/sequelize/lib/dialects/mysql/query.js:73:19)
at Query._callback (node_modules/sequelize/lib/dialects/mysql/query.js:37:22)
at Query.Sequence.end (node_modules/mysql/lib/protocol/sequences/Sequence.js:85:24)
at Query._handleFinalResultPacket (node_modules/mysql/lib/protocol/sequences/Query.js:144:8)
at Query.EofPacket (node_modules/mysql/lib/protocol/sequences/Query.js:128:8)
at Protocol._parsePacket (node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (node_modules/mysql/lib/protocol/Parser.js:74:12)
at Protocol.write (node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket. (node_modules/mysql/lib/Connection.js:109:28)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
at TCP.onread (net.js:531:20)
IMPORTANT NOTE: I tried the findAll query mentioned above with different versions of Sequelize. This issue couldn't be reproduced from version 3.1.1 to version 3.11.0
When using version 3.12.0 the issue appeared. Versions above 3.12.0 and until 3.19.2 (included) were not tested. As mentioned below, the Sequelize version that I intend to use is 3.19.3
Dialect: mysql
Database version: 5.5.49
Sequelize version: 3.19.3
Activity
janmeier commentedon Jun 20, 2016
Please create a fully self-contained test case (including creating test data), that is as small as possible - I tried to adapt your example but since I dont have any data in my tables it didn't crash
SSCCE
cweckesser commentedon Jun 20, 2016
Hi Jan,
I have just updated the issue by adding a section named "Test case that
reproduces the error", which contains a test case that reproduces the error.
I hope it works.
Regards.
On 20 June 2016 at 15:34, Jan Aagaard Meier [email protected]
wrote:
Carlos Weckesser
Backend Developer
[email protected]
cweckesser commentedon Jun 22, 2016
Hi @janmeier,
I've just updated the "Test case that reproduces the error" section in the
issue I created. It should be easier to execute.
Regards.
On 20 June 2016 at 17:33, Carlos Weckesser [email protected] wrote:
Carlos Weckesser
Backend Developer
[email protected]