postgresql - KnexJS Postgres:为原始查询添加额外的双引号
问题描述
考虑下面的代码:
const items = await Item.query().where(
"type",
"like",
raw("'??'", [`%${term}%`])
);
上面的代码我没有收到任何错误,但是数据库返回了一个空的结果集。创建的 SQL 查询如下:
select "items".* from "items" where type LIKE '%"mobiles"%'
请看上面SQL中的like mobiles'%"mobiles"%'
""
被视为值的一部分并返回一个空的结果集。
""
在上面的查询中如何避免?
编辑:请注意,我使用的 ObjectionJS 也使用了 Knex。
解决方案
??
应该用于列名。
我给你2个建议,
- 完全不使用查询
raw
,
const items = await Item.query().where('type', 'like', `%${term}%`);
- 使用单
?
,
const items = await Item.query().where('type', 'like', raw("'?'", [`%${term}%`]));
推荐阅读
- python - 无法根据特定字符串选择行
- node.js - ng serve - 此版本的 CLI 仅与 Angular ^13.0.0 兼容
- python - Tensorflow 在 .map 函数中访问 tensor.numpy() 但使用 py_function 会减慢迭代器的生成速度
- android - 在 Android 中编写响应正文的正确方法是什么?
- vue.js - 部署后,即使 yarn start 工作,Vue Nuxt SPA 也无法正确显示
- c - 比较两个字符串,如 strcmp 错误错误:一元'*'的无效类型参数(有'int')
- c# - ASMX 无法设置已知类型属性
- java - 如何使我的底页对话框动态化?
- java - 如何从 Postman 获取 POST 请求的令牌
- python - 使用 Scipy 生成并绘制正态分布 N[5,3²] 的概率密度函数 (PDF)