Skip to content
New issue

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

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

Already on GitHub? # to your account

Rails 7.1 composite keys in partitioned tables & returning :id #842

Open
pkopac opened this issue Aug 9, 2024 · 1 comment
Open

Rails 7.1 composite keys in partitioned tables & returning :id #842

pkopac opened this issue Aug 9, 2024 · 1 comment

Comments

@pkopac
Copy link

pkopac commented Aug 9, 2024

SomeModel represents partitioned table (by partition_key), where primary key is %w[id partition_key] so when I do this:

        SomeModel.import!(
          array_of_attributes,
          on_duplicate_key_update: { conflict_target: %i[id partition_key], columns: %i[...] },
          returning: %i[id some other columns]
        ).results

it worked fine in Rails 7.0, but in Rails 7.1 when calling id= it expects eg. a Hash, not just the value of id. So, for compatibility I added this:

module CompositeKeyCompatibility
  extend ActiveSupport::Concern

  # Compatibility override for Rails 7.1 composite key and activerecord-import (1.7.0) gem
  # When using returning it tries to set id=123, but this method requires a Hash.
  def id=(value)
    value = self.class.primary_key.map { |col| col == 'id' ? value : public_send(col) } unless value.is_a? Enumerable
    super
  end
end

But the library should probably do something similar so that it works out of the box.

@jkowens
Copy link
Collaborator

jkowens commented Oct 4, 2024

Thanks for reporting this issue. We'll need to ensure backwards compatibility so we'll probably need a version check to make sure this logic is only for AR 7.1+.

I'm thinking we could probably insert some logic in the set_value lambda:

set_value.call(model, single_column, result)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants