2

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?

2
  • Can you add a breakpoint to your controller and inspect the new Category object before it is saved? Commented Oct 10, 2016 at 0:13
  • I am in production and the bug only appears there. I added the params sent. Commented Oct 10, 2016 at 11:51

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

The ouput of your command was => ["id", "created_at", "updated_at"]. I believe the not null requirement comes from the Category model where I have validates :name, presence: true
That would not raise an ActiveRecord PG error, though. You'd get an error on #save! but #save (no bang on the end) would simply return false.
I'm afraid that I'm out of ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.