Reputation: 1047
In production, I have a model Category. When I try to create a category in the website I get the following error
ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR: null value in column "name" violates not-null constraint
The migration looks like this
class CreateCategories < ActiveRecord::Migration
def up
create_table :categories do |t|
t.references :supercategory, index: true
t.string :name
t.timestamps null: false
end
Category.create_translation_table! name: :string
end
....
end
And the schema
create_table "categories", force: :cascade do |t|
...
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.index ["restaurant_id"], name: "index_categories_on_restaurant_id", using: :btree
t.index ["slug"], name: "index_categories_on_slug", unique: true, using: :btree
t.index ["supercategory_id"], name: "index_categories_on_supercategory_id", using: :btree
end
Any ideas what might be causing the error? Thanks! If you need more code just ask.
EDIT The params sent
Parameters: {"utf8"=>"✓", "authenticity_token"=>"no3uGNSYkgClG+y6y9Y=", "category"=>{"name"=>"Coffee", "age_restriction"=>"0", "available_all_day"=>"0", "supercategory_id"=>"", "avatar"=>#, @original_filename="coffee.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"category[avatar]\"; filename=\"coffee.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Category", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, /; q=0.01", "restaurant_id"=>"kilimanjaro-coffee"}
EDIT I tried to create a category from the console and I found that the model does not have a name column. Seems like it has not been migrated properly. I dropped, created and migrated the database again but the column still does not appear. Any ideas where does that come from?
Upvotes: 2
Views: 5893
Reputation: 52356
It's difficult to imagine how you could get that error without the PostgreSQL table having that column set to be not null, so I suspect that you are not using the database that you think you are using.
You can check which columns active record believes are set to not null with this:
Category.columns.reject{|c| c.null}.map(&:name)
Upvotes: 1