我正在使用Sequelize将一个项目从MySQL移动到Postgres,并且有一件事情已经失败了。这看起来是因为“用户”不包含在任何地方。
编辑:错误消息是'缺少表用户的FROM子句条目'。
sequelize的代码是:
List.model.findOne({
where: {
id: listId,
$or: [
{ open: true },
['Users.id = ?', [userId]], // <-- the line I think is breaking this.
],
},
include: [{
model: db.models.User
}],
});
我认为这是因为User
条款中没有包含FROM
。但是,我不确定如何让Sequelize做正确的事。
Sequelize生成的查询是:
SELECT "List"."id",
"List"."name",
"List"."image",
"List"."slug",
"List"."open",
"List"."password",
"List"."createdAt",
"List"."updatedAt",
"List"."OwnerId",
"Users"."id" AS "Users.id",
"Users"."firstName" AS "Users.firstName",
"Users"."lastName" AS "Users.lastName",
"Users"."email" AS "Users.email",
"Users"."password" AS "Users.password",
"Users"."image" AS "Users.image",
"Users"."facebookId" AS "Users.facebookId",
"Users"."googleId" AS "Users.googleId",
"Users"."createdAt" AS "Users.createdAt",
"Users"."updatedAt" AS "Users.updatedAt",
"Users.UserList"."createdAt" AS "Users.UserList.createdAt",
"Users.UserList"."updatedAt" AS "Users.UserList.updatedAt",
"Users.UserList"."ListId" AS "Users.UserList.ListId",
"Users.UserList"."UserId" AS "Users.UserList.UserId"
FROM "Lists" AS "List"
LEFT OUTER JOIN ("UserList" AS "Users.UserList"
INNER JOIN "Users" AS "Users" ON "Users"."id" = "Users.UserList"."UserId") ON "List"."id" = "Users.UserList"."ListId"
WHERE "List"."id" = 13
AND ("List"."open" = TRUE
OR (Users.id = 1234));
答案 0 :(得分:4)
尝试用Users.id
这样的双引号包装"Users"."id"
,因为在PostgreSQL中,所有未加引号的标识符(表名,字段)都将折叠为小写,PostgreSQL区分大小写:https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
引用标识符也会使其区分大小写,而不带引号的名称始终折叠为小写。例如,标识符FOO,foo和&#34; foo&#34; PostgreSQL被认为是相同的,但&#34; Foo&#34;和&#34; FOO&#34;不同于这三者和彼此。 (在PostgreSQL中将不带引号的名称折叠为小写字母与SQL标准不兼容,后者表示不带引号的名称应折叠成大写。因此,foo应该等同于&#34; FOO&#34; not&#34; foo&#34;根据标准。如果你想编写便携式应用程序,建议你总是引用一个特定的名称或从不引用它。)