3

I am currently working on ruby on rails 3 tutorial by michael hartl. I am running into this problem when I try to call db:migrate. Could someone help me figure out why its aborting. Thanks!

** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Invoke db:load_config (first_time) ** Execute db:load_config ** Execute db:migrate == AddEmailUniquenessIndex: migrating ======================================== -- add_index(:users, :email, {:unique=>true}) rake aborted! 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")

CODE

class AddEmailUniquenessIndex < ActiveRecord::Migration
  def up
    add_index :users, :email, :unique => true
  end

  def down
    remove_index :users, :email
  end
end

USER CODE

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :email, :name, :password, :password_confirmation

  email_regex = /\A[\W+\-.]+@[a-z\d\-.]+\.|[a-z]+\z/i

  validates :name, :presence => true,
                   :length => { :maximum => 50 }
  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }
  validates :password, :presence => true,
                       :confirmation => true,
                       :length => { :within => 6..40 }


end

1 Answer 1

11

Nothing wrong with your migration. The error simply means you have existing email duplicates data in db.

Check your users table, set unique emails for existing rows or delete these rows. Then run the migration again.

Update: Please note that even if you remove the unique constraint from migration and add validates_uniqueness_of :email to your Active Model, the problem would still eat you in the future.

The root problem is your data is in a 'bad' state. If you have two rows having the same email address (or it's also possible they both have blank email), after adding validates_uniqueness_of :email your User model instance for these two rows won't be valid. So it's still a data problem you have to fix.

3
  • I am really new to using rails, and I am not sure what you mean exactly.
    – AustinT
    Commented Feb 8, 2013 at 3:50
  • @AustinTruong There are User records in your database which have the same email. This is the cause of the error while migrating. Delete or alter the duplicate emails in your database and then you should be able to run the migration. Commented Feb 8, 2013 at 5:33
  • Thank you. It was indeed the duplicate entry of email that caused the issue. I removed the entry using the sqlite db browser and then ran the script again.
    – codingbbq
    Commented Mar 24, 2013 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.