0

I'm new to Ruby on Rails and I'm following the https://www.railstutorial.org/book/ guide to understand a bit more about it. I'm stuck at 6.3.3, where it tells to create a secure password. The previous migration seemed to have worked (to create a unique index and to create the secure password column). After that when i try to run:

rake test

it says that the test was aborted and that i should run:

rake db:migrate RAILS_ENV=test

but when i do run the command above it returns this:

c:\Sites\sample_app>rake db:migrate RAILS_ENV=test

DL is deprecated, please use Fiddle
== 20141226095217 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true}) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::ConstraintException: indexed columns are not unique: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")c:/Si tes/sample_app/db/migrate/20141226095217_add_index_to_users_email.rb:3:in `change' C:in `migrate' Tasks: TOP => db:migrate (See full trace by running task with --trace)

I don't understand why this is happening. My DB is empty.

As requested, the migrations files:

20141226095217_add_index_to_users_email.rb

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

20141226095746_add_password_digest_to_users.rb

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string
  end
end
5
  • please show us db/migrate/20141226095217_add_index_to_users_email.rb and previos migration file too. Commented Dec 26, 2014 at 11:57
  • 1
    Errors say that you already have index on email. Remove unique: true from AddIndexToUsersEmail migration. Commented Dec 26, 2014 at 12:11
  • thanks @Зелёный, your suggestion worked, but it is strange since I haven't define uniqueness before that. Well, thanks anyway =) Commented Dec 26, 2014 at 12:23
  • just check actual db/scheme.rb for your database schema before run generate migration. Commented Dec 26, 2014 at 12:24
  • possible duplicate of Rake Aborted , on add_index(:users, :email, {:unique=>true}) Commented Dec 26, 2014 at 15:23

1 Answer 1

1

See https://stackoverflow.com/a/14765346/429758 for the exact same issue. As mentioned there, the issue is not with the migration, but with having duplicate values for email field in the users table.

Since this error is happening while running tests, it means the test database has duplicate emails for users.

Rails Tutorial book uses fixtures to setup test data. The test/fixtures/users.yml file used to create the users on test env is shown in Listing 6.29 as follows:

one:
  name: MyString
  email: MyString

two:
  name: MyString
  email: MyString

Both of these fixtures having MyString as email is what is causing the migration to fail. Change the values to make sure both have different values.

Example:

one:
  name: First User
  email: [email protected]

two:
  name: Second User
  email: [email protected]

EDIT

Looking back at the Rails Tutorial, the next step in Listing 6.30 is to empty the test/fixtures/users.yml file. That is another way to ensure this error does not occur.

Sign up to request clarification or add additional context in comments.

Comments

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.