using unions with "with" clause
I am trying to do something like the following, but cant figure out how to do this with drizzle syntax.
11 Replies
i'd like to do something like
await db.with(usersSubQuery).union(prefsSubQuery, recurringPrefsSubQuery);
i opened a GH issue with it, since I dont think this is possible https://github.com/drizzle-team/drizzle-orm/issues/2230
GitHub
[FEATURE]: Add ability to use CTEs with UNION queries · Issue #2230...
Describe what you want When peforming a union, I would like to be able to use the "with" clause to define a CTE. For example: const usersSubQuery = db .$with('sq') .as(getUserSubs...
Use the builder pattern
@Sillvva that query in the Union block throws an error that it doesn’t know about the usersSubQuery
PostgresError: relation "usersSubQuery" does not exist
it's wrapping everything up until the union in parensHmm. I'm not sure.
does that query you posted work for you just fine?
I haven't tried it with query functions like that. I don't have any I can work with right now to test it. The docs show this builder pattern though. (Click the
builder-pattern
tab in the code block)
https://orm.drizzle.team/docs/set-operations#unionDrizzle ORM - Set Operations
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
I'm not sure why it wouldn't work with
with
@Sillvva I got around to trying this today and it doenst work. you have to specify either
db.select
or db.with(sq).select
for the right side of the union. Either way, drizzle wraps the entire left side in its own parens and the entire right side in its own parens, so the CTE is scoped within the parens. I tried using a query builder object too, but same issue
@alexblokh @Andrew Sherman any ideas on this?I thinks it's a bug, we will work on fixing it. Also if someone wants to take it - feel free to file a PR
Thank you!