我对使用复合主键的模型有一些问题。看看我的代码
迁移
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
我的模特
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
问题是因为我无法使用data_areas关系。
我无法通过此测试。
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
当我检查关系失败时的最后断言。
Failure/Error: it { expect( @data_set.data_areas.size ).to eq 5 }
expected: 5
got: 0
我做错了什么?我在SO上阅读了有关类似问题的所有主题,但它对我没有帮助。
为了帮助您了解我的问题,我使用同样的问题创建了一个简单的应用
https://github.com/Draqun/composite_primary_keys_as_foreign_key_example
在规范中存在应该通过的测试。