Chained include generating incorrect SQL
Hi
I am not sure if the problem is my ignorance or if the problem is related to Sequelize. I am trying to load associated items by using the following code:
models.Document.find({
where:{documentID:30},
include:[
{model: models.Revision,
order:[["revisionNumber","DESC"]],
limit:1,
include:{model:models.SoftCopy}
}
]
}).then(function(data){
res.status(200).send({message:"OK",data:data});
})
The following query is being generated:
SELECT
`Revision`.*, `SoftCopies`.`softCopyID` AS `SoftCopies.softCopyID`,
`SoftCopies`.`revisionID` AS `SoftCopies.revisionID`,
`SoftCopies`.`softCopyTypeID` AS `SoftCopies.softCopyTypeID`,
`SoftCopies`.`createdAt` AS `SoftCopies.createdAt`,
`SoftCopies`.`updatedAt` AS `SoftCopies.updatedAt`
FROM
(
(
SELECT
`revisionID`,
`documentID`,
`revisorID`,
`createdAt`,
`updatedAt`
FROM
`Revisions` AS `Revision`
WHERE
`Revision`.`documentID` = 30
ORDER BY
`Revision`.`revisionNumber` DESC
LIMIT 1
)
) AS `Revision`
LEFT OUTER JOIN `SoftCopies` AS `Revisions.SoftCopies` ON `Revisions.revisionID` = `Revisions.SoftCopies`.`revisionID`;
The problem is with the AS for the JOIN. The alias does not match the table alias used in the SELECT. E.g.
For the JOIN: AS Revisions.SoftCopies,
for the SELECT: SoftCopies.revisionID
Another problem is that in the ON clause this: Revisions.revisionID should rather be: Revisions.revisionID.
Is this a bug, or am I missing something?
Thank you!
Please show your model definitons and associations
Hi, here is the model definition for the Revision:
module.exports = function (sequelize, DataTypes) {
var Revision = sequelize.define("Revision", {
revisionID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
documentID:{
type:DataTypes.INTEGER
},
revisorID: {
type: DataTypes.INTEGER
}
}, {
classMethods: {
associate: function (models) {
Revision.hasMany(models.SoftCopy,{
foreignKey:"revisionID"
})
}
}
});
return Revision;
}
For the SoftCopy:
module.exports = function (sequelize, DataTypes) {
var SoftCopy = sequelize.define("SoftCopy", {
softCopyID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
revisionID: {
type: DataTypes.INTEGER
},
softCopyTypeID: {
type: DataTypes.INTEGER
}
}, {
classMethods: {
associate: function (models) {
SoftCopy.belongsTo(models.Revision, {
foreignKey: "revisionID"
});
}
}
});
return SoftCopy;
}
Hope this is sufficient. (I left out irrelevant fields for brevity)
Please add the document model and the relationship between document and revision.
Here is a diagram of the relationships up to document.

The model definition for document is as follows:
module.exports = function (sequelize, DataTypes) {
var Document = sequelize.define("Document", {
documentID: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
classMethods:{
associate: function (models) {
Document.hasMany(models.Revision, {
foreignKey: "documentID"
});
}
}
});
return Document;
}
Possible a duplicate of #4605
By removing the "limit:1" from the find, I was able to retrieve all the revisions with their related soft copies, however I would like to have a way to only get the latest revision's soft copies.
I will try creating a work-around for now.
Yeah it's a problem with separate and nested includes i think, track #4605
Thank you. Will do.