On Wed, Aug 9, 2023 at 11:35 AM aryk....@gmail.com <aryk.gr...@gmail.com> wrote:
> Hi Jeremy, > > I saw some historical posts about batch updates like this one: > > https://groups.google.com/g/sequel-talk/c/uV7PRbEvQj0/m/lCvZaLx8AAAJ > > It feels a little "hacky" to say insert this record with a unique primary > key that already exists...when you get an error, then update it. > > I've been using a solution like this: > > run <<-SQL > update user_expo_push_tokens as uept set -- postgres FTW > device_id = ud2.device_id > from (values > #{updates.join(", ")} > ) as ud2(installation_id, device_id) > where ud2.installation_id = uept.installation_id; > SQL > > This also works in the case where the values are not a primary key or have > a unique index to throw the "on duplicate key update" logic. > > Is there a way to accomplish this without having to drop to straight sql? > I think this will work: DB.from(Sequel.as(:user_expo_push_tokens, :uept), DB.values(updates).as(:ud2, [:installation_id, :device_id])). where{{ud2[:installation_id]=>uept[:installation_id]}}. update(:device_id => Sequel[:ud2][:device_id]) SQL: UPDATE "user_expo_push_tokens" AS "uept" SET "device_id" = "ud2"."device_id" FROM (VALUES ...) AS "ud2"("installation_id", "device_id") WHERE ("ud2"."installation_id" = "uept"."installation_id") It depends on what "updates" is supposed to be. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CADGZSSdKw2L6_hpfZVFBZT9udCCeXa29FUpdQxbvt08fc5HraA%40mail.gmail.com.