Reputation: 111
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
Upvotes: 0
Views: 628
Reputation: 13057
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]
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.
Upvotes: 1