2016-09-26 64 views
0

我正在使用Sequelize將項目從MySQL移動到Postgres,並且有一件事情直接失敗了。這看起來是因爲「用戶」不在任何地方。Where子句在`OR`語句中使用包含關係

編輯:錯誤消息是'缺少表用戶的FROM-clause條目'。

代碼爲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

什麼是錯誤消息? –

+0

缺少來自表用戶的FROM子句條目 – David

回答

4

嘗試用雙引號包裹Users.id"Users"."id",因爲在PostgreSQL中所有未加引號的標識符(表名,字段)將被摺疊成小寫和PostgreSQL是病例敏感:https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

引用標識符也會區分大小寫,而未加引號的名稱總是會摺疊爲小寫。例如,PostgreSQL認爲標識符FOO,foo和「foo」是相同的,但是「Foo」和「FOO」與這三者和彼此不同。 (在PostgreSQL中將未加引號的名字摺疊爲小寫字母與SQL標準不兼容,這表示非加引號的名字應該摺疊爲大寫字母,因此,根據標準,foo應該等同於「FOO」而不是「foo」。如果您想編寫便攜式應用程序,建議您始終引用特定名稱或從不引用它。)

+0

我剛剛離開代碼,但這看起來很像它。該錯誤消息甚至將表名稱還原爲小寫。一旦我有機會嘗試一下,我會盡快接受。 – David

+0

就是這樣。我不確定你如何在沒有錯誤信息的情況下到達那裏,但謝謝! – David