Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate Check Error When Using import! Method on Rails 7.2.0.beta2 #840

Closed
k-tsuchiya-jp opened this issue Jun 24, 2024 · 3 comments · Fixed by #845
Closed

Duplicate Check Error When Using import! Method on Rails 7.2.0.beta2 #840

k-tsuchiya-jp opened this issue Jun 24, 2024 · 3 comments · Fixed by #845

Comments

@k-tsuchiya-jp
Copy link

k-tsuchiya-jp commented Jun 24, 2024

Issue Summary:

Using the import! method to update existing data now triggers a duplicate check error.

Environment:

activerecord-import: 1.7.0
rails: 7.2.0.beta2
ruby: 3.1.6

Details:

  • import! method is equipped with the on_duplicate_key_update option.
  • The target Model for update includes a uniqueness validation.
class Book < ApplicationRecord
  validates :isbn_13, uniqueness: true
end

upsert_attributes = params.map | param | { Book.new(isbn_13: param.isbn_13, title: param.title) }
Book.import! upsert_attributes, on_duplicate_key_update: [:title]
@k-tsuchiya-jp
Copy link
Author

k-tsuchiya-jp commented Jun 24, 2024

It seems that @compiled is now included in the return value of klass._validate_callbacks.
https://github.com/zdennis/activerecord-import/blob/v1.7.0/lib/activerecord-import/import.rb#L36

      @compiled=
       #<ActiveSupport::Callbacks::Filters::Before:0x00007fb51ac6b980
        @filter=
         #<ActiveRecord::Validations::UniquenessValidator:0x00007fb51ac646f8
          @attributes=[:isbn_13],
          @klass=
           Book(id: integer, isbn_13: string, title: string, created_at: datetime, updated_at: datetime),
          @options={}>,
        @halted_lambda=
         #<Proc:0x00007fb5255f75a8 /usr/local/bundle/gems/activesupport-7.2.0.beta2/lib/active_support/callbacks.rb:665>,
        @name=:validate,
        @user_callback=
         #<Proc:0x00007fb51ac6b9d0 /usr/local/bundle/gems/activesupport-7.2.0.beta2/lib/active_support/callbacks.rb:384 (lambda)>,
        @user_conditions=[]>,

It appears that @compiled has been added to the return value starting from the following commit:
Commit Link

@jkowens
Copy link
Collaborator

jkowens commented Aug 23, 2024

Thanks for reporting this @KojiTsuchiya-Ruby!

@jkowens
Copy link
Collaborator

jkowens commented Aug 23, 2024

Resolved by #845.

@jkowens jkowens closed this as completed Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants