gpt4 book ai didi

node.js - 自定义查询的不一致的 Sequelize 查询构造

转载 作者:行者123 更新时间:2023-12-03 22:33:26 25 4
gpt4 key购买 nike

我目前正在尝试在我的 Node 应用程序中构建一个 Sequelize 查询,如下所示;

import sequelize from 'sequelize';

function getReadMsg(){
const where = [sequelize.where(
sequelize.literal('"Message"."status" = \'read\'')
)];

const attributes = { exclude: ['updated_at'] };

const order = [['created_at', 'ASC']];

return model.findAll({ where, attributes, order }); // model is a sequelize model object
}
但是,Sequelize 构造的原始查询正在比较
其中 块为空,因此违背了查询的目的。
下面是原始的 Sequelize 构造查询;
SELECT "id", "title", "body", "to", "from", "status", "created_at" FROM "Messages" AS "Message" WHERE ("Message"."status" = 'read' IS NULL) ORDER BY "Message"."created_at" ASC;
代替;
SELECT "id", "title", "body", "to", "from", "status", "created_at" FROM "Messages" AS "Message" WHERE ("Message"."status" = 'read') ORDER BY "Message"."created_at" ASC;
压缩版本:5.21.4。
squelize-cli 版本:5.5.1。
Node 版本:12.16.1

最佳答案

sequelize.where 使用 3 个参数,如果省略最后两个参数,它们将分别为 '=' 和 'null'。
你应该做这样的事情:

sequelize.where(sequelize.literal('"Message"."status"'), '=', 'read')

关于node.js - 自定义查询的不一致的 Sequelize 查询构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64610911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com