Kuasta
Kuasta

Reputation: 41

Knex - updating rows where value from select

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

List:

Attributes:

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?

Upvotes: 4

Views: 4647

Answers (1)

Mikael Lepistö
Mikael Lepistö

Reputation: 19728

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']))
})

Upvotes: 5

Related Questions