title: Ruby on Rails SQLite3::ConstraintException: NOT NULL constraint failed:
body: <p>I am developing a simple app where a user can add a subject to a cart. Before I add the authentication I was able to add a subject to the cart but as I want a user to be able to has access to just his/her cart I used Devise to create User with authentication. Now, when I click on the button to add a subject to the cart I have the following error: </p>
  
  <p>This is a snapshot of the error I get: <a href="http://i.stack.imgur.com/71IIy.png" rel="nofollow">1</a></p>
  
  <blockquote>
    <p>SQLite3::ConstraintException: NOT NULL constraint failed: carts.user_id: INSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?)</p>
  </blockquote>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
  
    private
  
      def current_cart
        Cart.find(params[:user_id])
      rescue ActiveRecord::RecordNotFound
        cart = Cart.create
        params[:user_id] = cart.id
        cart
      end
  end
  
  
      class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_one :cart
  end
  
  class Cart &lt; ActiveRecord::Base
    belongs_to :user
    has_many :line_items, dependent: :destroy
  
    scope :user_carts, -&gt;(user) { where(['user_id= ?', user.id]) }
  
  end
  
  class AddUsersToCarts &lt; ActiveRecord::Migration
  
    def up
      add_reference :carts, :user, index: true
      Cart.reset_column_information
      user = User.first
  
      Cart.all.each do |cart|
        cart.user_id = user.id
        cart.save!
      end
  
      change_column_null :carts, :user_id, false
      add_foreign_key :carts, :users
    end
  
    def down
      remove_foreign_key :carts, :users
      remove_reference :carts, :user, index: true
    end
  
  end
  </code></pre>
  
  <p>Edit: I added the schema.rb below:</p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20151210213408) do
  
    create_table "carts", force: :cascade do |t|
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.integer  "user_id",    null: false
    end
  
    add_index "carts", ["user_id"], name: "index_carts_on_user_id"
  
    create_table "line_items", force: :cascade do |t|
      t.integer  "subject_id"
      t.integer  "cart_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    create_table "subjects", force: :cascade do |t|
      t.string   "title",       null: false
      t.string   "code",        null: false
      t.text     "description", null: false
      t.integer  "credits",     null: false
      t.string   "lecturer",    null: false
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
    end
  
    add_index "subjects", ["title"], name: "index_subjects_on_title", unique: true
  
    create_table "users", force: :cascade do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at",                          null: false
      t.datetime "updated_at",                          null: false
    end
  
    add_index "users", ["email"], name: "index_users_on_email", unique: true
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  
  end
  </code></pre>