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 key support #142

Open
freibuis opened this issue Oct 22, 2023 · 2 comments
Open

Rails 7.1 Composite key support #142

freibuis opened this issue Oct 22, 2023 · 2 comments

Comments

@freibuis
Copy link

As the title says...
Rails now supports Composite keys,

would be nice to be able to supply an array of items as the key otherwise use :id using the terser-syntax

Model.seed([:field_one,:field_two], *data)

if this currently happens you will get the follow error

 Your seed constraints contained unknown columns: `[:field_one, :field_two]`. Valid columns are: `field_one`, `field_two`
@Jakanapes
Copy link

I get a TypeError: can't quote Array when just trying to run seeds on an existing table/model with composite primary keys

@freibuis
Copy link
Author

freibuis commented Mar 14, 2024

I have located were the issue is.

lib/seed-fu/seeder.rb

change in the def new
@constraints = constraints.to_a.empty? ? [:id] : constraints
to
@constraints = constraints.to_a.empty? ? [:id] : [constraints].flatten

the issue is in def update_id_sequence
@model_class.sequence_name.nil?

change

 if @model_class.connection.adapter_name == "PostgreSQL" or @model_class.connection.adapter_name == "PostGIS"
          return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?

to

 if @model_class.connection.adapter_name == "PostgreSQL" or @model_class.connection.adapter_name == "PostGIS"
          return if @model_class.primary_key.is_a?(Array)
          return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?

then seed like this

id = 1
tennant_id =1
data = [{
 id: [ tennant_id,id],
 field1: 'foo',
 field2: 'bar'
 }]
Model.seed_once([:tennant_id,:id], *data)
# depending on how you built your primary composite key you might be able to still use
Model.seed_once( :id, *data)

hope this helps

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

2 participants