question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updates with joins in postgres

See original GitHub issue

when 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:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
duzenkocommented, Nov 2, 2020

This worked for me

	await knex( 'managers' ).update( {
		address: knex( 'clients' ).select( 'address' ).where( 'id', knex.raw( '??', ['managers.clientId'] ) ),
	} );
2reactions
elhigucommented, Jul 26, 2017

knex doesn’t support from clause 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):

knex.from('table').update({
  a: knex('othertable').select('a').where('b', knex.raw('??', ['table.b'])
})
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found