我目前正在研究 michael hartl 的 ruby on rails 3 教程。当我尝试调用 db:migrate 时遇到了这个问题。有人可以帮我弄清楚它为什么会中止。谢谢!
** 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")
代码
class AddEmailUniquenessIndex < ActiveRecord::Migration
def up
add_index :users, :email, :unique => true
end
def down
remove_index :users, :email
end
end
用户代码
# == 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