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

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

Open
pkopac opened this issue Aug 9, 2024 · 0 comments
Open

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

pkopac opened this issue Aug 9, 2024 · 0 comments

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.

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

No branches or pull requests

1 participant