使用左连接和 json agg 时类型不正确 (Postgres)
作者: probablykabari创建于 2024年5月9日更新于 2026年9月12日
标签postgrestypescript
const result = kysely
.selectFrom("some_table")
.leftJoin("another_table", "some_table.id", "another_table.some_table_id")
.select((eb) => [
eb.fn
.jsonAgg("another_table")
.as("another_records"),
...
在上面的示例中,`json_agg`函数调用的结果的类型 `TypeScript` 是不正确的,因为在`left join`中,json数组中的值可能为 `[ null ]`,如果没有联接记录。要解决这个问题,您需要添加 `.filterWhere("another_table.id", "is not", null)`,这将使得聚合的结果仅为 `null`,但这在库中也是不正确的。内容来源: kysely-org/kysely