activerecord-import icon indicating copy to clipboard operation
activerecord-import copied to clipboard

Support on_duplicate_key_update for recursive imports

Open ramblex opened this issue 2 years ago • 5 comments

Hello, this PR adds support for specifying separate on_duplicate_key_update options for associations when doing a recursive import. I couldn't find a way of doing that with the existing code - the closest seemed to be passing in :all but that didn't quite do what I needed. Apologies if I've missed if this is already possible..

The PR adds a new recursive_on_duplicate_key_update option to import where on_duplicate_key_update options can be specified for each of the associations. The new option takes a Hash as its value where the keys are the table names and the values are the options normally passed to on_duplicate_key_update.

In the example below [:id] is used as the on_duplicate_key_update option when importing topics and { conflict_target: [:id], columns: [:author_name] } is used as the on_duplicate_key_update option when importing books.

Topic.import new_topics,
              recursive: true,
              on_duplicate_key_update: [:id],
              recursive_on_duplicate_key_update: {
                books: { conflict_target: [:id], columns: [:author_name] }
              }

Many thanks for this fantastic gem..!

ramblex avatar Aug 27 '22 14:08 ramblex

hey @ramblex , thanks for adding this functionality to gem, but can you plz tell me if your code has capability to handle n number of associations with nested child also? like book has many topics and topic has many sub_topics

saqlainriaz avatar Jan 23 '23 23:01 saqlainriaz

@ramblex i think if you make it recursive then it may fix this problem

saqlainriaz avatar Jan 23 '23 23:01 saqlainriaz

I am in need of this feature. For me, it's just one level of nesting.

railsfactory-dineshr avatar Jun 23 '23 11:06 railsfactory-dineshr

Hi @saqlainriaz, recursive_on_duplicate_key_update supports specifying which columns to update for nested associations using something like the following where the tree is topics -> books -> chapters.

Topic.import new_topics,
              recursive: true,
              on_duplicate_key_update: [:id],
              recursive_on_duplicate_key_update: {
                books: { conflict_target: [:id], columns: [:author_name] },
                chapters: { conflict_target: [:id], columns: [:title] }
              }

I've added in a test to show it working. One possible issue, however, is that the leaf objects (chapters in the example) are only imported if the intermediate objects (books in the example) have been changed, but that seems to be a more general issue - on_duplicate_key_update: :all has the same behaviour.

ramblex avatar Jun 24 '23 08:06 ramblex

This would also greatly benefit us

john-h-k avatar Aug 01 '23 16:08 john-h-k

+1 It would be nice! Is there any update? @zdennis @jkowens

marciondg avatar May 03 '24 17:05 marciondg

this would help us too

mculp avatar May 16 '24 19:05 mculp

Released in v1.7.0!

jkowens avatar May 17 '24 22:05 jkowens