mbleigh / seed-fu

Advanced seed data handling for Rails, combining the best practices of several methods together.
MIT License
1.23k stars 155 forks source link

Rails 7.1 Composite key support #142

Open freibuis opened 1 year ago

freibuis commented 1 year ago

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 commented 11 months ago

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

freibuis commented 7 months ago

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