Knex(使用 Expressjs)和 postgresSQL:字符串在 SQL 中被 knex 自动双引号
Knex(with Expressjs) and postgresSQL: String is automatically double quoted in SQL by knex
当我尝试使用 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 的单引号 hello ,如下所示?
and "tm2"."menu_path" = 'hello'
而不是
and "tm2"."menu_path" = "hello"
是的,来自the docs:
If you need to use a literal value (string, number, or boolean) in a join instead of a column, use 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]))
当我尝试使用 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 的单引号 hello ,如下所示?
and "tm2"."menu_path" = 'hello'
而不是
and "tm2"."menu_path" = "hello"
是的,来自the docs:
If you need to use a literal value (string, number, or boolean) in a join instead of a column, use 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]))