当我试图使用knex在SQL中传递一个值时,我遇到了一个问题。
app.get('/home/:topbarMenuPath', (req, res)=> {
const { topbarMenuPath } = req.params;
var toStringQuery =
db.select('tm1.menu_name','tm1.seq','tm1.menu_path','tm1.menu_id')
.from('tb_menu as tm1')
.join('tb_menu as tm2', function() {
this.on('tm1.parent_menu_id', '=', 'tm2.menu_id')
.andOn('tm2.menu_level', '=', 1)
.andOn('tm1.menu_level', '=', 2)
.andOn('tm2.menu_path','=',topbarMenuPath)
}).toString()
;
console.log('toStringQuery',toStringQuery);
});如果我将':topbarMenuPath‘作为'hello’传递,console.log输出将显示如下:
select "tm1"."menu_name", "tm1"."seq", "tm1"."menu_path", "tm1"."menu_id"
from "tb_menu" as "tm1"
inner join "tb_menu" as "tm2" on "tm1"."parent_menu_id" = "tm2"."menu_id"
and "tm2"."menu_level" = 1
and "tm1"."menu_level" = 2
and "tm2"."menu_path" = "hello"Postgres似乎无法识别双引号"hello",当我试图将SQL发送到Postgres时,它显示了一个错误。错误:
{
"length": 166,
"name": "error",
"severity": "ERROR",
"code": "42703",
"position": "251",
"file": "d:\\pginstaller_12.auto\\postgres.windows-x64\\src\\backend\\parser\\parse_relation.c",
"line": "3359",
"routine": "errorMissingColumn"
}有任何方法可以得到SQL单引号如下图所示吗?
and "tm2"."menu_path" = 'hello'而不是
and "tm2"."menu_path" = "hello"发布于 2020-08-16 13:59:30
是的,来自医生们
如果需要在联接中而不是在列中使用文字值(字符串、数字或布尔值),请使用knex.raw。
knex.select('*').from('users').join('accounts', 'accounts.type', knex.raw('?', ['admin']))
Outputs:
select * from `users` inner join `accounts` on `accounts`.`type` = 'admin'对你来说,这意味着.andOn('tm2.menu_path', '=', knex.raw('?', [topbarMenuPath]))
https://stackoverflow.com/questions/63436630
复制相似问题