-1
期間唯一約束如何避免Duplicate key value violates unique constraint
重複鍵值違反rspec的
我認爲id:1
已在使用,但我需要強烈設置id
,
,因爲我有方法category
模型
def iconic
case self.id
when 1
smth
....
end
end
我的工廠
FactoryGirl.define do
factory :category do
sequence(:title) { |n| Faker::Hipster.word+"#{n}" }
position 1
text Faker::Lorem.sentence
image File.open(Rails.root.join('test', 'assets', 'images', 'banners', (1..6).to_a.sample.to_s+'.png'))
end
end
Failure/Error: c1 = create(:category, id: 1)
ActiveRecord::RecordNotUnique:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "categories_pkey"
DETAIL: Key (id)=(1) already exists.
什麼是實際使用情況? – max