我有以下错误,当试图使用FactoryGirl创建具有p = FactoryGirl.create(:flight)
航班:FactoryGirl的has_many协会
ActiveRecord::InvalidForeignKey:
PG::ForeignKeyViolation: ERROR: insert or update on table "flights" violates foreign key constraint "fk_rails_11f6e1e673"
DETAIL: Key (customer_id)=(457) is not present in table "customers".
: INSERT INTO "flights" ("flight_type", "route", "customer_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
我的模型如下:
flight.rb
belongs_to :customer
customer.rb
has_many :flights
这里是我在Factories.rb:
factory :customer do
customer_name 'Customertest'
contract_type 'true'
end
factory :flight do
flight_type 'Medivac'
route 'A - B - C - A'
customer
end
你明白为什么它不工作?
感谢
所以你想做的事是当你创建**航班**其与**客户关联** – MZaragoza