Updates with joins in postgres
See original GitHub issuewhen compiling to mysql there is a clear syntax for joining to an update. However, in postgres the syntax for doing this is update...from, which from what I understand knex can’t really support, because there can only be one primary table. is it possible to compile this ;
knex.from('table').update({a:knex.raw('othertable.a')}).join('othertable', 'othertable.b', 'table.b')
to this;
update table set a = othertable.a from othertable where othertable.b = table.b
particularly when the knex instance is for postgres? I understand that the syntax restricts us to inner joins, so limiting the join methods that work to innerJoin would be reasonable, but at the moment this just silently fails to add anything related to the join, and trying to add a from clause just changes the primary table.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
PostgreSQL UPDATE Join with A Practical Example
To join to another table in the UPDATE statement, you specify the joined table in the FROM clause and provide the join condition...
Read more >How to do an update + join in PostgreSQL? - Stack Overflow
The UPDATE syntax is: [ WITH [ RECURSIVE ] with_query [, ...] ] UPDATE [ ONLY ] table [ [ AS ] alias...
Read more >How UPDATE JOIN works in PostgreSQL? - eduCBA
To join another table in the statement, we have to define the PostgreSQL FROM clause with the joined table, along with we need...
Read more >PostgreSQL update join vs SQL Server update join
In T-SQL I would do an update using a join using something like this: UPDATE foo SET bar = t2. bar FROM foo...
Read more >Documentation: 15: UPDATE - PostgreSQL
When using FROM you should ensure that the join produces at most one output row for each row to be modified. In other...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This worked for me
knex doesn’t support
fromclause for updates / deletes etc. it works only for select.However you can do something this (there might be some syntax errors… didn’t run the query):