我正在使用 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));
最佳答案
尝试用双引号将 Users.id
包裹起来,例如 "Users"."id"
,因为在 PostgreSQL 中所有不带引号的标识符(表名、字段)都会被折叠为小写且 PostgreSQL 区分大小写:https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)
关于javascript - 在 `OR` 语句中使用包含关系的 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39702650/