import! method is not raising when unique constraint violated while still inserting some records
luismaldonadov opened this issue · 6 comments
Hi, hope you can help me out
Im doing a import like this:
result = Model.import!(array_of_records, on_duplicate_key_ignore: true)
collect_stuff(array_of_records)
The Model
has a unique constraint db-side that is comprised of 3 fields, whenever a record in the array_of_records
contains a record that violated that constraint, the array_of_records
is left with only records without id
set (I suppose those are the failed ones) but the interesting part, is that the result
object contains ids of records that could be imported.
In a nutshell:
- The list of records is not synchronized
- I got a partial insert with no raise whatsoever
Thank you in advance
you are passing on_duplicate_key_ignore: true
, which means to ignore any record which is violating any uniqueness constraint without raising any error.
https://github.com/zdennis/activerecord-import?tab=readme-ov-file#duplicate-key-ignore
@salmanasiddiqui That part I understand, but what is somewhat weird is that the list of successful records is not synchronized, is that expected?
@luismaldonadov using the on_duplicate_key_ignore
option prevents ids from being set on the imported objects. See: https://github.com/zdennis/activerecord-import?tab=readme-ov-file#duplicate-key-ignore
@jkowens I get that, but maybe I was expecting a mixed list with records with id set and other without it. I understand if this is the current behavior, just wanted to bring this to everyone's attention. Having a list with no synchronized records while still inserting some is kind of unexpected from my perspective
Seems that is current behavior that will not change so I'm closing the ticket.
Yes, this cannot be changed. There is no way to know which records the returned ids belong to when inserted using on_duplicate_key_ignore
.