2017-07-14 110 views
0

尝试与FactoryGirl.create(:place)创建地方实例,但得到了以下错误:FactoryGirl:非空约束违反

Failure/Error: FactoryGirl.create(:place) 
ActiveRecord::StatementInvalid: 
    PG::NotNullViolation: ERROR: null value in column "town_city" violates not-null constraint 
    DETAIL: Failing row contains (1, 2017-07-14 13:32:41.246627, 2017-07-14 13:32:41.246627, 51.476900, 0.000010, Greenwich Observatory, Greenwich, London, null, 24849 Lesch Garden, null, null, null, MF4 1FO, null, null, null, null, 1, null, f). 
    : INSERT INTO "places" ("created_at", "geocode_address", "latitude", "longitude", "postcode", "region_id", "street_address", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" 
# ./spec/requests/api/v1/events_spec.rb:5:in `block (2 levels) in <top (required)>' 

想不通为什么,因为在我厂我设置town_city明确:

地方工厂:

factory :place, traits: [:pageable] do 
    town_city 'my hardcoded value' 
    latitude { rand(50.0...53.0) } 
    longitude { rand(-2.0...2.0) } 
    street_address { Faker::Address.street_address } 
    postcode { Faker::AddressUK.postcode } 
    association :region, factory: :region 
end 

页&可分页特点:

trait :pageable do 
    association :page, strategy: :build 
end 

factory :page do 
    sequence(:title) { |n| "#{Faker::Product.product_name}-#{n}" } 
    association :user 
    association :cover_image, factory: :image 
    telephone { Faker::PhoneNumber.phone_number } 
    description { generate :paragraphs_seq } 
end 

地区:

factory :region do 
    level 'area' 
end 

任何想法可能是什么原因呢?

回答

0

事实证明,我在我的地方的模型before_validation挂钩,这是尝试重置town_city使用地区字段中的数据。所以我想我必须修改我的地区工厂

相关问题