我尝试使用Postgres在Knex中实现以下查询,以便返回静态" $ type" column(用于向GraphQL服务器提供类型提示):
select *, 'Patrol' as "$type" from patrol;
当我使用Knex查询构建器时,它会修改引号:
knex('patrol')
.select(['*', `'Patrol' as "$type"`])
.where('id', 12345)
.first()
返回
ERROR: column "'Patrol'" does not exist at character 11
STATEMENT: select *, "'Patrol'" as """$type""" from "patrol" where "id" = $1 limit $2
我可以使用knex.raw()
构建查询,但我真的不想这样做:
knex.raw(
`SELECT *, 'Patrol' as "$type" FROM patrol WHERE id = '${value}' LIMIT 1;`
)
我应该如何构造select()
语句,以便查询构建器正确解释它?
答案 0 :(得分:2)
这不起作用(https://runkit.com/embed/g5h8qwmeyoyh)?
const Knex = require('knex');
const knex = Knex({
client: 'pg'
});
knex('patrol')
.select('*', 'Patrol as $type')
.where('id', 12345)
.toSQL()
// select *, "Patrol" as "$type" from "patrol" where "id" = ?
或者您是否真的尝试向每行添加别名为'$ type'的字符串文字Patrol
?如果是这样的方法可以像这样使方言转义/引用正确(https://runkit.com/embed/12av9qxxwgyj):
require('sqlite3');
const Knex = require('knex');
const knex = Knex({
client: 'sqlite',
connection: ':memory:'
});
await knex.schema.createTable('test', t => {
t.increments('id').primary();
t.string('data');
});
await knex('test').insert([{ data: 'foo' }, { data: 'bar' }]);
console.dir(
await knex('test').select('*', knex.raw('? as ??', ['Patrol', '$type']))
);
答案 1 :(得分:1)
我能够在'facebook' => [
'client_id' => 'xxxxxx',
'client_secret' => 'xxxxxxxxxxxxxx',
'redirect' => 'provide_the_redirect_url_here',
],
:
knex.raw()
来使其发挥作用
select