-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
postgres UPDATE - FROM syntax #1586
Comments
If you have that as a string in JS, just execute it using I don't think you can do it using knex's query building functions (though I could be mistaken :-)), at least because it would require specifying two main tables in the query, and knex's query objects support only one. You could always implement such an UPDATE using a sub-query instead of using the UPDATE/FROM SQL extension (which even gets implemented with different semantics by different databases), e.g. (warning: untested code :-)):
|
Thank you. But could you show me how to turn this script into knex query without update "transaction"
set "status" = 'completed'
from "return_transaction"
where "return_transaction"."id" = 1 and "transaction"."id" = "return_transaction"."transaction" |
I think Jurko just explained that knex cannot build you query like you are writing without This is might work though var subQuery = knex('transaction')
.select('transaction.id')
.join('return_transaction', 'transaction.id', '=', 'return_transaction.transaction')
.where('return_transaction.id', 1);
knex('transaction').update({'status' : 'completed'}).where('id', subQuery); I suppose that |
No it's not working. I get error I believe knex should support "UPDATE - FROM" syntax for postgres. |
I updated subquery there was ambiguous column identifier in Yep, that would be nice if update - from could be supported. If you decide to implement it, pull requests are always welcome. |
I don't understand how that makes for two main tables, you just need to add an "updateFrom" that simply adds a "from tableName" after the set, it's a simple string. Another option would be to add an options to the update method. this.knex.table("table1").update({id: 2}, {from: ["table2"]}) Or this.knex.table("table1").update({id: 2}).updateFrom(["table2"]); |
I have implemented a I was thinking to use the update options but there are so many overloads I found it bloated. |
@onzag Do you still have the code for your |
@ffxsam sadly not anymore, I ended up moving away from knex and wrote my own wrapper on top of pg as I found more limitations, I never uploaded that change anywhere. |
@onzag No worries, thanks for responding! |
I have a PR open (#5386) that implements this. |
Released in 2.5.0 |
Is there a way to write postgres script like this?
The text was updated successfully, but these errors were encountered: