Skip to content

recursive_on_duplicate_key_update and unique validations #841

@kalsan

Description

@kalsan

Hey there and thank you for this awesome gem!

I'm currently using it to import a compount key in the following form where Customer has_many :subscriptions:

import_result = Customer.import(
        customers,
        # validate_uniqueness: false, # TODO: why does this not help?
        validate: false, # TODO: why is this needed?
        recursive:                         true,
        on_duplicate_key_update:           {
          conflict_target: %i[imported_by_id imported_id],
          columns:         %i[
            foo
            bar
          ]
        },
        recursive_on_duplicate_key_update: {
          subscriptions: {
            # validate_uniqueness: false, # TODO: why does this not help?
            conflict_target: %i[imported_by_id imported_id],
            columns:         %i[
              morefoo
              morebar
            ]
          }
        }
      )

As you can see in the "TODO" marked parts above, I need to pass validate: false for the record to sucessfully import. Otherwise, the following validation in Subscription fails:

validates :imported_id, uniqueness: { scope: :imported_by_id }, allow_blank: true

I'm wondering whether this is a problem of understanding on my side or a bug.

Thanks a lot in advance!

Best,
Kalsan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions