Yuka Miya
Yuka Miya

Reputation: 31

SQLite3::ConstraintException: NOT NULL constraint failed: items.title: INSERT INTO "items" ("image", "created_at", "updated_at") VALUES (?, ?, ?)

I am developing a simple app where a user can add a subject to a cart. But there is an error about not null constraint.

The error message browser shows is like that.

SQLite3::ConstraintException: NOT NULL constraint failed: items.title: INSERT INTO "items" ("image", "created_at", "updated_at") VALUES (?, ?, ?)

I've tried deleting not null constraint in schema.rb. But the error message is still on. So, what should I do?

Schema:

create_table "items", force: :cascade do |t|
    t.string "image", null: false
    t.string "title", null: false
    t.string "description", null: false
    t.string "stock", null: false
    t.string "price", null: false
    t.integer "status", limit: 1, default: 0, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Controller:

class SellController < ApplicationController
  def new
    @item = Item.new
  end

  def confirm
  @item = Item.new(title: params[:title],price: params[:price],stock: params[:stock],description: params[:description],image: "default_item.jpg")
  render :new if @item.invalid?
  end

  def create
    @item = Item.new(title: params[:title],price: params[:price],stock: params[:stock],description: params[:description],image: "default_item.jpg")
    #@item = Item.new(item_params)
    if params[:image]
      @item.image = "#{@item.id}.jpg" 
      image = params[:image]
      File.binwrite("public/item_images/#{@item.image}", image.read)
    end
    if params[:back]
      format.html { render :new }
    elsif @item.save
      flash[:notice] = "your item data is saved."
      redirect_to("/sell/complete")
    else
      render("sell/new")
    end
  end

  def complete
  end
end

I expect item data be saved and the page on browser is changed to thanks page.

Upvotes: 3

Views: 1305

Answers (1)

mrzasa
mrzasa

Reputation: 23327

Take a closer look at your error message:

SQLite3::ConstraintException: NOT NULL constraint failed: items.title: INSERT INTO "items" ("image", "created_at", "updated_at") VALUES (?, ?, ?)

It says that failed not null constraint on items.title column. And indeed, you don't provide title in your insert statement. It means that they were not passed to Item.new in your controller code.

I can see two solutions - if you want to keep all those constraints (you have multiple not-null columns in your table), you should also add active record presence validations. They will prevent calling insert statement to the database if values are missing and they will give you nice error messages in @item.error.

If you can allow those columns to be nullable, you can drop the constraint in a migration:

change_column_null :items, :title, false

You can also rollback the migration that created items table and edit this migration to avoid setting NOT NULL constraint there.

schema.rb is generated when you run migrations and can be used to load schema into a database. It's not done automatically though, you need to run a proper rake task (rake db:schema:load). It's not advisable to edit this file manually, as it's auto-generated and your changes will be automatically overwritten.

Upvotes: 2

Related Questions