You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem: If you have a conditional upsert, record.upsert!({arel_condition: upsert_condition}), and the condition returns no rows, then the record object will be empty.
My situation: In my case, my condition is about timestamps. self.class.arel_table[:shopify_updated_at].lteq(send(:shopify_updated_at)) where Shopify is an external source of data for my app. I only want to upsert if the current record is newer (has a later updated_at).
Proposed solution: There are solutions listed in the Stack Overflow discussion. I might suggest a simple solution which is to follow up the INSERT with a subsequent read when nothing is returned. The read should also use the upsert_keys if given. This is necessary because otherwise it is not know how to find a matching record.
For me, the upsert_keys is usually an external ID column. Something like shopify_order_id. I don't want duplicate orders, so I upsert on conflict of duplicate shopify_order_id. If a conflict is found, and the shopify_updated_at of what I'm trying to upsert is older, then by the way Postgres works, no row will be returned! So the only thing to do is to either remove the condition, modify the INSERT query, or fetch again when empty.
Perhaps a do-no-harm short-term change could be to not overwrite @attributes when values.first.to_h is .empty? ? It won't really help fix anything but at least it's less data-destructive
Context: The problem is basically https://stackoverflow.com/questions/34708509/how-to-use-returning-with-on-conflict-in-postgresql/37543015
Problem: If you have a conditional upsert,
record.upsert!({arel_condition: upsert_condition})
, and the condition returns no rows, then therecord
object will be empty.My situation: In my case, my condition is about timestamps.
self.class.arel_table[:shopify_updated_at].lteq(send(:shopify_updated_at))
where Shopify is an external source of data for my app. I only want to upsert if the current record is newer (has a later updated_at).Proposed solution: There are solutions listed in the Stack Overflow discussion. I might suggest a simple solution which is to follow up the INSERT with a subsequent read when nothing is returned. The read should also use the
upsert_keys
if given. This is necessary because otherwise it is not know how to find a matching record.For me, the
upsert_keys
is usually an external ID column. Something likeshopify_order_id
. I don't want duplicate orders, so I upsert on conflict of duplicateshopify_order_id
. If a conflict is found, and theshopify_updated_at
of what I'm trying to upsert is older, then by the way Postgres works, no row will be returned! So the only thing to do is to either remove the condition, modify the INSERT query, or fetch again when empty.For example purposes only:
The text was updated successfully, but these errors were encountered: