Restrictions in union with with and withRecursive
See original GitHub issueThe postgres with recursive
query can refer to its own output. The syntax can look like this
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t;
The kysely union
method does only take a querybuilder as input which is not type aware of its surroundings.
Hence the following does not work:
db.withRecursive("t", qb =>
qb
.selectFrom(sql<{ n: number }>`(select 1 as n)`.as("q"))
.select("q.n")
.unionAll(qb =>
qb
.selectFrom("t")
.select(sql<number>`n+1`.as("n"))
.where("t.n", "<", 100)
)
);
because the unionAll
does not accept a querybuilder function.
What does work, but has less type support, is using the raw sql builder in the unionAll
db.withRecursive("t", qb =>
qb
.selectFrom(sql<{ n: number }>`(select 1 as n)`.as("q")
)
.select("q.n")
.unionAll(sql`select n+1 as n from t where t.n < 100`);
The restriction in the union
method makes it also harder to access the same with query in two union queries like this
with t as (
select 1 as n
)
select * from t
union all select * from t
Here, the following does not work:
db.with("t", qb =>
qb.selectFrom(sql<{ n: number }>`(select 1 as n)`.as("q")).select("q.n")
)
.selectFrom("t")
.select("n")
.unionAll(qb => qb.selectFrom("t").select("n"));
and even the following (which is not ideal) is not possible because of missing brackets in the resulting query
const qb1 = db
.with("t", qb =>
qb
.selectFrom(sql<{ n: number }>`(select 1 as n)`.as("q"))
.select("q.n")
)
.selectFrom("t")
.select("n");
const qb2 = db
.with("t", qb =>
qb
.selectFrom(sql<{ n: number }>`(select 1 as n)`.as("q"))
.select("q.n")
)
.selectFrom("t")
.select("n");
const qbUnion = qb1.unionAll(qb2);
which results in the following query
with "t" as (select "q"."n" from (select 1 as n) as "q") select "n" from "t" union all with "t" as (select "q"."n" from (select 1 as n) as "q") select "n" from "t"
throwing a syntax error:
syntax error at or near “with”
Are there any plans on allowing a callback function in the union
and unionAll
methods allowing us to have access to the typings?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
So I think here’s how to fix your query:
I’m not sure I understood your problem, but this works:
kysely is able to parse the
ancestors(name, parent)
name into a table that hasname
andparent
columns, and you are able to reference it inside and outside the callback.