postgresql Kysely Query with array of“From”with raw SQL

uqdfh47h  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(122)

我正在使用Kysely查询生成器(与Postgres),但是将Kysely等效应用于FROM X, Y似乎不起作用:
例如

SELECT *
FROM
  products,
    jsonb_array_elements(products.properties#>'{someProperty}') as search_item

where cast(value as integer)=2

字符串
我试着寻找等价的,并在源代码文档中看到:/node_modules/kysely/dist/esm/query-creator.d.ts:

import { sql } from 'kysely'

const items = await db.selectFrom([
    'person as p',
    db.selectFrom('pet').select('pet.species').as('a'),
    sql<{ one: number }>`(select 1 as one)`.as('q')
  ])
  .select(['p.id', 'a.species', 'q.one'])
  .execute()

The generated SQL (PostgreSQL):

select "p".id, "a"."species", "q"."one"
from
  "person" as "p",
  (select "pet"."species" from "pet") as a,
  (select 1 as one) as "q"


因此,我使用的语法利用sql对象,如第三个选择参数如下:

const res2 = await db.selectFrom([
    'products',
    sql<{ someProperty: number }>`(jsonb_array_elements(products.properties#>'{someProperty}')`.as('search_item')
  ]).select(['products.properties', 'search_item']).execute();


但是得到一个错误:

error: Uncaught (in promise) PostgresError: syntax error at end of input
          error = new PostgresError(parseNoticeMessage(current_message));
                  ^
    at Connection.#simpleQuery (https://deno.land/x/[email protected]/connection/connection.ts:700:19)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async Connection.query (https://deno.land/x/[email protected]/connection/connection.ts:949:16)
    at async PoolClient.#executeQuery (https://deno.land/x/[email protected]/client.ts:245:12)
    at async PoolClient.queryObject (https://deno.land/x/[email protected]/client.ts:433:12)
    at async PostgreSQLDriverDatabaseConnection.executeQuery (https://deno.land/x/[email protected]/src/PostgreSQLDriverDatabaseConnection.ts:14:12)
    at async https://esm.sh/v133/[email protected]/denonext/kysely.mjs:3:50936
    at async Or.provideConnection (https://esm.sh/v133/[email protected]/denonext/kysely.mjs:3:86791)
    at async t.executeQuery (https://esm.sh/v133/[email protected]/denonext/kysely.mjs:3:50891)
    at async t.execute (https://esm.sh/v133/[email protected]/denonext/kysely.mjs:3:58522)


有什么想法吗?看起来就像是

sxpgvts3

sxpgvts31#

最后,有一个额外的(被翻译成无效的查询。正确的术语是:

sql<{ bestPlayerCount: number }>`jsonb_array_elements(products.properties#>'{someProperty}')`.as('search_item')

字符串
我通过搜索deno存储缓存文件的位置找到了它:

~/.deno/bin/deno info --json https://deno.land/x/[email protected]/mod.ts | grep connection.ts -B 10


修改文件以在执行之前打印生成的查询,同时还从deno.lock中删除匹配文件postgres@../connection/connection,因此不会因校验和不匹配而失败。

相关问题