【发布时间】:2015-01-08 21:08:12
【问题描述】:
我遇到了一个问题,在我的 Factory 上调用 create 时出现 Null 违规错误。
这是我的两个模型:
# Table name: test_masters
# id :integer not null, primary key
class TestMaster < ActiveRecord::Base
has_one :test_slave, dependent: :destroy
end
# Table name: test_slave
# id :integer not null, primary key
# test_master_id :integer not null
class TestSlave < ActiveRecord::Base
belongs_to :test_master, dependent: :destroy
end
还有以下工厂:
FactoryGirl.define do
factory :test_master do
test_slave
end
end
FactoryGirl.define do
factory :test_slave do
end
end
当我在 rails 控制台中运行 FactoryGirl.create(:test_master) 时,出现以下错误:
PG::NotNullViolation: ERROR: null value in column "test_master_id" violates not-null constraint
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "test_master_id" violates not-null constraint
我认为从 test_master 工厂调用 test_slave 工厂会自动提供 test_master_id...但似乎并非如此。
我的工厂有问题吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 associations factory-bot