Rails - composite primary key as foreign key - relationship not working
I have a problem with models with composite primary key. Look at my code
Migration
class CreateDataSets < ActiveRecord::Migration
def change
create_table :data_sets do |t|
t.integer :synchronization_report_id, null: true
t.string :configuration_id, null:false, limit: 100
t.datetime :synch_time, null: false
t.timestamps null: false
end
execute "ALTER TABLE data_sets DROP CONSTRAINT data_sets_pkey;"
execute "ALTER TABLE data_sets ADD CONSTRAINT data_sets_pkey PRIMARY KEY (id, synchronization_report_id);"
execute "ALTER TABLE data_sets ADD FOREIGN KEY (synchronization_report_id) REFERENCES synchronization_reports (id);"
end
end
class CreateDataAreas < ActiveRecord::Migration
def change
create_table :data_areas do |t|
t.string :configuration_id, null: false, limit: 100
t.integer :data_set_id, null: true
t.integer :synchronization_report_id, null: true
t.string :status, null: false, limit: 20
t.string :error_type, null: true, limit: 100
t.string :error_text, null: true
t.string :sql, null: true
t.integer :records_files_processed, null: false
t.timestamps null: false
end
execute "ALTER TABLE data_areas DROP CONSTRAINT data_areas_pkey;"
execute "ALTER TABLE data_areas ADD CONSTRAINT data_areas_pkey PRIMARY KEY (id, data_set_id, synchronization_report_id);"
execute "ALTER TABLE data_areas ADD FOREIGN KEY (data_set_id, synchronization_report_id) REFERENCES data_sets(id, synchronization_report_id);"
end
end
My Models
class SynchronizationReport::DataSet < ActiveRecord::Base
self.table_name = :data_sets
self.primary_keys = :id, :synchronization_report_id
belongs_to :synchronization_report
has_many :data_areas, class_name: SynchronizationReport::DataArea, dependent: :delete_all
validates_presence_of :synchronization_report_id, :configuration_id, :synch_time
validates_length_of :configuration_id, in: 2..100
end
class SynchronizationReport::DataArea < ActiveRecord::Base
self.table_name = :data_areas
self.primary_keys = :id, :data_set_id, :synchronization_report_id
belongs_to :data_set, foreign_key: [:data_set_id, :synchronization_report_id]
belongs_to :synchronization_report
validates_inclusion_of :status, in: %w[imported import_failed exported export_failed]
validates_presence_of :configuration_id, :data_set_id, :synchronization_report_id, :status, :records_files_processed
validates_length_of :configuration_id, in: 3..100
validates_length_of :status, in: 3..20
validates_length_of :error_type, in: 3..100, allow_nil: true
end
The problem is that I cannot use the data_areas relation.
I cannot pass this test.
RSpec.describe SynchronizationReport::DataSet, type: :model do
describe "data_areas relation" do
before(:all) do
(0...5).each do |i|
FactoryGirl.create(:data_area, data_set_id: @data_set.id[0][1], synchronization_report_id: @data_set.synchronization_report.id)
end
end
it { should have_many(:data_areas) }
it { expect( @data_set.data_areas.size ).to eq 5 }
end
end
One last statement when I check for relationship failure.
Failure/Error: it { expect( @data_set.data_areas.size ).to eq 5 }
expected: 5
got: 0
What am I doing wrong? I have read all threads with similar questions on SO, but it didn't work for me.
To help you understand my problem, I created a simple app with this same problem
https://github.com/Draqun/composite_primary_keys_as_foreign_key_example
There is a test in the spec that needs to be passed.
+3
source to share
No one has answered this question yet
Check out similar questions: