gpt4 book ai didi

mysql - 使用 "Unknown Column"和 "limit"选项时,Sequelize 出现 "include"错误

转载 作者:行者123 更新时间:2023-12-01 06:29:57 24 4
gpt4 key购买 nike

我有一个相对复杂的 Sequelize 查询,它导致“错误:ER_BAD_FIELD_ERROR:‘where 子句’中的未知列‘EventImage.EventId’”。我可以清楚地看到生成的 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'
}
]
})

where = "'Image'.'Category'='gallery' AND 'EventImage'.'EventId' in (1,2)"
offset = 0
rows = 12
orderby = "Image.createdAt DESC"



Sequelize 使用以下选项初始化:

underscored: false,
freezeTableName: true,
paranoid: true,
syncOnAssociation: true,
charset: 'utf8',
collate: 'utf8_general_ci',
timestamps: 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 图:
enter image description here

任何帮助,将不胜感激。

最佳答案

我不确定它是否仍然需要它,但是对于那些受苦的人,请尝试添加subQuery: false到查询。 Source.

如果它不起作用,请尝试添加 separate: true到子查询。 Source.

希望它有所帮助,gl。

关于mysql - 使用 "Unknown Column"和 "limit"选项时,Sequelize 出现 "include"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23312868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com