续集关系为空

新手上路,请多包涵

我对续集有疑问。这是我正在尝试做的事情:

 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 做到这一点?我尝试了许多不同的方法,但没有一种有效…

原文由 Patrick Mettraux 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 263
1 个回答

如果您使用 Sequelize.Op 中的 Sequelize 的符号运算符,那么…

 return pointsTransaction.findAndCountAll({
    where:{
        user_id: userId
    },
    limit: opts.limit,
    offset: opts.offset,
    include:[{
        model:sequelize.models.pointsPendingtransaction,
        where: {
            transaction_id: {
              // "$eq" changes to "[Op.eq]"
              [Op.eq]: null
            }
        }
    }]
});

原文由 Jeremiah S 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏