Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex - updating rows where value from select

I am using an sqlite3 DB and have these two tables.

List:

  • ID
  • OriginID

Attributes:

  • ID
  • ListID
  • OriginList

Attributes.OriginList == List.OriginID

I need to update rows in Attributes to match the current List.ID. I am using Knex and my current raw query looks like this:

UPDATE Attributes 
SET ListID = (SELECT ID 
              FROM List 
              WHERE OriginID = Attributes.OriginList)

I just started with DBs and have been trying to come up with ways to do this without raw, but haven't been successful. Is there such a way?

like image 780
Kuasta Avatar asked Oct 18 '25 15:10

Kuasta


1 Answers

Something like this should create the RAW query mentioned in OP:

knex('Attributes').update({ 
  ListID: knex('List').select('ID').where('OriginID', knex.raw('??', ['Attributes.OriginList']))
})
like image 157
Mikael Lepistö Avatar answered Oct 20 '25 03:10

Mikael Lepistö