KnexJS Postgres:为类似原始的查询添加额外的双引号

KnexJS Postgres: Adds extra double quotation marks to raw like query

考虑以下代码:

const items = await Item.query().where(
      "type",
      "like",
      raw("'??'", [`%${term}%`])
);

我没有收到上面代码的任何错误,但是数据库 returns 一个空结果集。创建的 SQL 查询如下:

select "items".* from "items" where type LIKE '%"mobiles"%'

请看上面的 like mobiles SQL '%"mobiles"%' "" 被视为值的一部分并且 returns 一个空结果集。

如何避免上面查询中的""

编辑:请注意,我也在使用使用 Knex 的 ObjectionJS。

?? 应该用于列名。

我有 2 条建议给你,

  1. 使用完全没有 raw 的查询,
const items = await Item.query().where('type', 'like', `%${term}%`);
  1. 使用单个 ?,
const items = await Item.query().where('type', 'like', raw("'?'", [`%${term}%`]));