2013-02-08 3 views
2

저는 현재 michael hartl의 레일 3 튜토리얼에서 루비 작업을하고 있습니다. db : migrate를 호출하려고하면이 문제가 발생합니다. 누군가가 내가 왜 낙담하는지 알아낼 수있게 도와 줄 수 있을까? 감사!레이크 중단되었습니다, add_index (: users, : email, {: unique => true})

** 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 

사용자 코드 마이그레이션 문제

# == 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+\-.][email protected][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 

답변

10

아무것도. 오류는 단순히 기존의 전자 메일 중복 데이터를 db로 가지고 있다는 것을 의미합니다.

사용자 테이블을 확인하거나 기존 행에 고유 한 이메일을 설정하거나 삭제할 수 있습니다. 그런 다음 마이그레이션을 다시 실행하십시오.

업데이트 : 마이그레이션에서 고유 제한 조건을 제거하고 validates_uniqueness_of :email을 (를) 활성 모델에 추가하더라도 문제가 계속 발생할 수 있습니다.

루트 문제는 데이터가 '불량'상태라는 것입니다. 동일한 이메일 주소를 사용하는 두 개의 행이있는 경우 (또는 둘 다 비어있는 이메일을 가질 수도 있음) validates_uniqueness_of :email을 추가 한 후이 두 행에 대한 User 모델 인스턴스가 유효하지 않습니다. 따라서 여전히 해결해야 할 데이터 문제입니다.

+0

난 정말 레일을 사용하는 것이 처음이므로 정확히 무슨 뜻인지 확실하지 않습니다. – AustinT

+0

@AustinTruong 데이터베이스에는 동일한 전자 메일이있는 사용자 레코드가 있습니다. 이것은 마이그레이션하는 동안 오류의 원인입니다. 데이터베이스의 중복 된 전자 메일을 삭제하거나 변경하면 마이그레이션을 실행할 수 있습니다. –

+0

고맙습니다. 실제로 문제를 일으킨 이메일의 중복 된 항목이었습니다. sqlite db 브라우저를 사용하여 항목을 제거한 다음 스크립트를 다시 실행했습니다. – noobcode

관련 문제