簡體   English   中英

where子句在OR語句中使用包含關系

[英]Where clause using included relation in `OR` statement

我正在使用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"包裝起來,例如"Users"."id" ,因為在PostgreSQL中所有未加引號的標識符(表名,字段)都將折疊為小寫,並且PostgreSQL區分大小寫: https://www.postgresql。組織/文檔/電流/靜態/ SQL的語法lexical.html#SQL-SYNTAX的標識符

引用標識符也使其區分大小寫,而未引用的名稱總是折疊為小寫。 例如,PostgreSQL認為標識符FOO,foo和“ foo”相同,但是“ Foo”和“ FOO”彼此不同。 (在PostgreSQL中,將未加引號的名稱折疊成小寫是與SQL標准不兼容的,SQL標准說,未加引號的名稱應該被折疊成大寫。因此,根據標准,foo應該等於“ FOO”而不是“ foo”。如果您想編寫可移植的應用程序,建議您始終使用特定名稱,也不要使用任何特定名稱。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM