如何从 Sequelize 查询中删除双引号? [英] How to remove double quotes from Sequelize query?
问题描述
我正在使用带有 Sequelize 的 Node/Express 来查询我的 sqlite 数据库,并且在尝试查询 JSON 类型的列时,有多余的双引号 ("
) 被添加到查询中阻止查询返回任何内容.
I'm using Node/Express with Sequelize to query my sqlite database and when trying to query a column of type JSON, there are superfluous double-quotes ("
) being added to the query which block the query from returning anything.
我用来使用 Sequelize 进行查询的代码:
The code I'm using to make the query using Sequelize:
sites = await Site.findAll({
where: {
[Op.or]: [
'title', 'site_url', 'tags'
].map(key => ({
[key]: {
[Op.like]: `%${search}%`
}
}))
}
});
结果(简化的)查询如下所示:
The resulting (simplified) query looks like this:
SELECT * FROM `Sites` AS `Site` WHERE (`Site`.`title` LIKE '%min%'
OR `Site`.`site_url` LIKE '%min%' OR `Site`.`tags` LIKE '"%min%"');
注意双引号:`Site`.`tags` LIKE '"%min%"'
在没有双引号的情况下进行手动查询时,查询会按预期工作.那么如何使用 Sequelize 删除它们?
When doing a manual query without the double-quotes, the query works as intended. So how can I remove them using Sequelize?
推荐答案
能够找到解决此问题的方法.而不是:
Was able to figure out a solution for this problem. Instead of:
[Op.like]: `%${search}%`
只需添加方括号:
[Op.like]: [`%${search}%`]
这样做是为了让 Sequelize 可以在 JSON 中搜索,同时也可以像平常一样搜索其他列类型,例如 varchar
.
made it so that Sequelize will search within the JSON while also searching other column types such as varchar
like normal.
这篇关于如何从 Sequelize 查询中删除双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!