Knex where 不允许将单个字符串传递给“.where()”

Knex where doesn't allow passing single string to `.where()`

我在 Node.js 使用 Knex 和 SQLite 编写的程序中有以下行:

await db.table("books")
         .innerJoin("items", "items.id", "books.item_id")
         .with("idx", db.raw(`instr(items.name, ?) asc`, name))
         .where("idx > 0")
         .orderBy("idx")
         .select()

其中db是通过调用knex(config)创建的变量。但是,函数 raw(sql) 似乎不起作用,因为它在运行时不断抛出此错误:

TypeError: The operator "undefined" is not permitted at Formatter.operator (I:\git\server\node_modules\knex\lib\formatter.js:138:13)

at QueryCompiler_SQLite3.whereBasic (I:\git\server\node_modules\knex\lib\query\compiler.js:525:100)

at QueryCompiler_SQLite3.where (I:\git\server\node_modules\knex\lib\query\compiler.js:314:32)

我做错了什么?

如果相关的话,我正在用 Typescript 编写,正如您在 await 中看到的那样,我正在使用 ES6。但是,如果我排除 with() 并且 with() 拒绝接受不是由 raw().

创建的内容,则此查询执行良好

编辑:

如果我测试这个,它表明问题出在 with() 而不是 raw():

console.log("name: " + name);
console.log("db.raw(name): " + db.raw(`instr(items.name, ?) asc`, name));

给出预期的输出。

原来问题出在 where(),直到我更仔细地检查堆栈跟踪才发现。用 .where("idx", ">", 0) 替换 .where("idx > 0") 修复了它。