Hello
I'm using Sequel to map a legacy database and Fabrication for the factories in my tests.
I have the following model definitions:
class DNSKeyRecord < Sequel::Model
set_dataset :dnskey_records
set_primary_key :id
one_to_one :rrsig_record, class: :RRSigRecord, key: :dnskey_record_id
end
class RRSigRecord < Sequel::Model
set_dataset :rrsig_records
many_to_one :dnskey_record, class: :DNSKeyRecord
end
The database constraints enforce the presence of rrsig_record_id in table dnskey_records entries and of dnskey_record_id in the rrsig_records table entries.
I managed to work around the cyclic associations by using Fabricate.build and an after_build hook in the dnskey_record factory:
Fabricator(:rrsig_record, from: :RRSigRecord) do
dnskey_record { Fabricate.build(:dnskey_record) }
end
Fabricator(:dnskey_record, from: :DNSKeyRecord) do
after_build do |record|
record.rrsig_record = Fabricate.build(:rrsig_record, dnskey_record: record)
end
end
However, when running rspec this generates the following error:
Sequel::Error:
associated object #<DNSKeyRecord @values={}> does not have a primary key
I can make the error go away by setting a fake id for the records with
id { Faker::Number.number(6) }
Is this expected? Is there a cleaner way around this?
Thanks,
Andre