Rails Tutorial: SQLite3::ConstraintException: UNIQUE constraint failed: users.email

16.9k views Asked by At

I'm following along with the rails tutorial. I'm on ch. 6 and I'm getting a strange error with SQLite3 (for the record, I'm using sqlite version 1.3.10 and the tutorial uses 1.3.9)

I don't get an error when I run rake db:migrate, but when I run the migration for the test environment, here's what I get:

$ bundle exec rake test:models

rake aborted!
ActiveRecord::PendingMigrationError:

Migrations are pending. To resolve this issue, run:

    bin/rake db:migrate RAILS_ENV=test

sample_app/test/test_helper.rb:3:in `<top (required)>'
sample_app/test/models/user_test.rb:1:in `require'
sample_app/test/models/user_test.rb:1:in `<top (required)>'
Tasks: TOP => test:models
(See full trace by running task with --trace)


$ bundle exec rake db:migrate RAILS_ENV=test

== 20150628011937 AddIndexToUsersEmail: migrating ===========================
==
    -- add_index(:users, :email, {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:


SQLite3::ConstraintException: UNIQUE constraint failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constrain
t failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users"
("email")
sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
SQLite3::ConstraintException: UNIQUE constraint failed: users.email
sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Here is my user.rb model:

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
end

Here is my most recent migration

  class AddIndexToUsersEmail < ActiveRecord::Migration
    def change
      add_index :users, :email, unique: true
    end
  end

I can post any other files if those are relevant. Thanks in advance for the help!

6

There are 6 answers

4
Ben Muschol On BEST ANSWER

The problem was that I had users in the database with the same email before the migration.

db:reset solved everything

0
epicrato On

If this happens to you when running tests:

Make sure there are no users fixtures, unless you explicitly define those users with unique attributes.

test/fixtures/users.yml

When you generate a model, for example:

rails g model User

Rails automatically creates that file and it looks like:

# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

This can mess up your tests.

0
Hanzo Hasashi On

I had to delete my development.db to resolve the issue.

0
Sarge On

Just use rake db:test:prepare and it will copy the dev database to test so you don't need to run the migrations.

4
Mailo Světel On

I experienced it for a bit different reason and here is the solution.

For me the error was

Minitest::UnexpectedError: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2018-05-09 18:23:59.827561', '2018-05-09 18:23:59.827561', 298486374)

Notice the INTO "users" ("created_at", "updated_at", "id"). It was caused by test/fixtures/users.yml was not filled, so then there was duplicate values in columns which has unique key constraint.

test/fixtures/users.yml:

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
0
Moshe Edri On

if you can't reset:db you should try to delete it by hand go to db folder and delete the "development.sqlite3" and the "test.sqlite3" files then run rails db:migrate and bin/rails db:migrate RAILS_ENV=TEST

worked for me, anyway...