我有一个相对复杂的Sequelize查询导致"错误:ER_BAD_FIELD_ERROR:未知列' EventImage.EventId'在' where子句'"。我可以清楚地看到SQL中生成的问题,但我不知道如何解决它。
错误是准确的,因为在子查询中生成的WHERE子句引用了未包含在子查询中的列。据我所知,Sequelize正在生成子查询作为实现行限制的方法。
我使用sequelize@2.0.0-dev11,MySQL 5.5.35-0ubuntu0.13.10.2和Node v0.10.21。
以下是基本代码:
var orm = require('../model');
var i = orm.Image;
i.findAll({
where: where,
offset: offset,
limit: rows,
order: orderby,
include: [{
model: orm.User,
as: 'User'
},
{
model: orm.Event,
as: 'Events'
},
{
model: orm.Comment,
as: 'Comments'
},
{
model: orm.Favorite,
as: 'Favorites'
}
]
})
其中="'图片'。'类别' ='图库' AND' EventImage'。' EventId'在 (1,2)"
偏移= 0
行= 12
orderby =" Image.createdAt DESC"
使用以下选项初始化Sequelize:
强调:false,
freezeTableName:true,
偏执:真,
syncOnAssociation:true,
charset:' utf8',整理: ' utf8_general_ci',
时间戳:true
以下是生成的SQL:
SELECT
`Image` . *,
`User`.`id` AS `User.id`,
`User`.`LoginName` AS `User.LoginName`,
`User`.`FirstName` AS `User.FirstName`,
`User`.`LastName` AS `User.LastName`,
`User`.`EmailAddress` AS `User.EmailAddress`,
`User`.`ProfileImage` AS `User.ProfileImage`,
`User`.`Password` AS `User.Password`,
`User`.`Enabled` AS `User.Enabled`,
`User`.`Expiry` AS `User.Expiry`,
`User`.`createdAt` AS `User.createdAt`,
`User`.`updatedAt` AS `User.updatedAt`,
`User`.`deletedAt` AS `User.deletedAt`,
`Events`.`id` AS `Events.id`,
`Events`.`StartDate` AS `Events.StartDate`,
`Events`.`EndDate` AS `Events.EndDate`,
`Events`.`Title` AS `Events.Title`,
`Events`.`Description` AS `Events.Description`,
`Events`.`createdAt` AS `Events.createdAt`,
`Events`.`updatedAt` AS `Events.updatedAt`,
`Events`.`deletedAt` AS `Events.deletedAt`,
`Events`.`UserId` AS `Events.UserId`,
`Events`.`ImageId` AS `Events.ImageId`,
`Events.EventImage`.`createdAt` AS `Events.EventImage.createdAt`,
`Events.EventImage`.`updatedAt` AS `Events.EventImage.updatedAt`,
`Events.EventImage`.`ImageId` AS `Events.EventImage.ImageId`,
`Events.EventImage`.`EventId` AS `Events.EventImage.EventId`,
`Comments`.`id` AS `Comments.id`,
`Comments`.`Body` AS `Comments.Body`,
`Comments`.`createdAt` AS `Comments.createdAt`,
`Comments`.`updatedAt` AS `Comments.updatedAt`,
`Comments`.`deletedAt` AS `Comments.deletedAt`,
`Comments`.`UserId` AS `Comments.UserId`,
`Comments`.`ImageId` AS `Comments.ImageId`,
`Comments`.`EventId` AS `Comments.EventId`,
`Favorites`.`id` AS `Favorites.id`,
`Favorites`.`createdAt` AS `Favorites.createdAt`,
`Favorites`.`updatedAt` AS `Favorites.updatedAt`,
`Favorites`.`UserId` AS `Favorites.UserId`,
`Favorites`.`ImageId` AS `Favorites.ImageId`
FROM
(SELECT
`Image` . *
FROM
`Image` AS `Image`
WHERE
`Image`.`Category` = 'gallery'
AND `EventImage`.`EventId` in (2)
AND `Image`.`deletedAt` IS NULL
LIMIT 12) AS `Image`
LEFT OUTER JOIN
`User` AS `User` ON `User`.`id` = `Image`.`UserId`
LEFT OUTER JOIN
`EventImage` AS `Events.EventImage` ON `Image`.`id` = `Events.EventImage`.`ImageId`
LEFT OUTER JOIN
`Event` AS `Events` ON `Events`.`id` = `Events.EventImage`.`EventId`
LEFT OUTER JOIN
`Comment` AS `Comments` ON `Image`.`id` = `Comments`.`ImageId`
LEFT OUTER JOIN
`Favorite` AS `Favorites` ON `Image`.`id` = `Favorites`.`ImageId`
ORDER BY Image.createdAt DESC;
以下是相关表格的EER图表:
任何帮助都将不胜感激。