2015-09-28 34 views
4

我遇到了sequelize問題。這裏是我想要做的事:在關係爲空的情況下會出現sequelize

return pointsTransaction.findAndCountAll({ 
    where:{ 
     user_id: userId 
    }, 
    limit: opts.limit, 
    offset: opts.offset, 
    include:[{ 
     model:sequelize.models.pointsPendingtransaction 
    }] 
}); 

生成的查詢看起來像這樣:

SELECT "pointsTransaction".*, 
"pointsPendingtransactions"."transaction_ptr_id" AS "pointsPendingtransactions.transactionPtrId", 
"pointsPendingtransactions"."is_active" AS "pointsPendingtransactions.isActive", 
"pointsPendingtransactions"."transaction_id" AS "pointsPendingtransactions.transaction_id" 
FROM (
SELECT "pointsTransaction"."id", 
"pointsTransaction"."date_time" AS "dateTime", 
"pointsTransaction"."details", 
"pointsTransaction"."points", 
"pointsTransaction"."user_id" 
FROM "points_transaction" AS "pointsTransaction" 
WHERE "pointsTransaction"."user_id" = 10002 LIMIT 1000 
) AS "pointsTransaction" 
LEFT OUTER JOIN "points_pendingtransaction" AS "pointsPendingtransactions" 
ON "pointsTransaction"."id" = "pointsPendingtransactions"."transaction_id" 

所以在SQL我只需要添加該行我查詢的結尾,使它的工作:WHERE "pointsPendingtransactions"."transaction_id" IS null

所以我的問題是,我怎麼能做到這一點sequelize?我試過很多不同的方式,但沒有一個工作......

+1

喜歡的東西: 包括:[{ 模型: sequelize.models.pointsPendingtransaction, 其中:{transaction_id:null} }]應該工作儘管我還沒有試過這個m我自己 – Molda

+1

我試了一下,也嘗試了一些基於計數但沒有任何工作。我懷疑由於「findAndCountAll」導致的錯誤 –

+0

當您嘗試Molda的建議時,您會得到什麼錯誤? –

回答

0

嘗試做下一個

 where: { 
      transaction_id: { 
       $eq: null 
      } 
     } 

產生IS NULL


return pointsTransaction.findAndCountAll({ 
    where:{ 
     user_id: userId 
    }, 
    limit: opts.limit, 
    offset: opts.offset, 
    include:[{ 
     model:sequelize.models.pointsPendingtransaction, 
     where: { 
      transaction_id: { 
       $eq: null 
      } 
     } 
    }] 
}); 
相關問題