Independent data-modifying CTEs in Postgres #6569
Unanswered
SF-300
asked this question in
Usage Questions
Replies: 1 comment 1 reply
|
this use case has never been presented here and unfortunately the quality of Stack Overflow is quite low these days and we no longer recommend it for support. CTE objects are basically included in WITH whenever they are referred towards by the select() in any way, and we would almost think here that select_from(cte) would handle it but you don't want the CTE referred towards in the FROM clause either. Adding the CTE to select_from() while also setting flags on the cte() to "hide" it from the FROM list has the effect of it not being considered for the WITH clause. so right now the CTE can get pulled in pretty much by setting a hint for it, but I would not consider this to be first class API: from sqlalchemy import table, select, column
t = table('t', column('c1'), column('c2'))
ins = t.insert().returning(t.c.c1, t.c.c2).cte()
stmt = select(t).with_hint(ins, "text")
print(stmt) |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hello!
First let me thank the community for such an amazing piece of software! It's really super cool to have such open-source things floating around)
I have a question regarding CTEs handling in Postgres dialect. According to Postgres' documentation, data-modifying CTEs are guaranteed to be executed even if they are not referenced in the final statement at all:
However I'm having hard time implementing this using query DSL from sqlalchemy (and it seems that I'm not alone in this). Basically I'm looking for something which would translate into query like this:
Am I missing something obvious? Or is it really impossible to express this via sqlalchemy? As for now I have to resort to final dummy SELECT statement which somehow references all data-modifying CTEs and it's... not pretty.
Any help would be greatly appreciated! Thanks in advance!
All reactions