迁移问题

时间:2019-03-23 15:54:08

标签: ruby-on-rails database

我正在学习RoR,现在我想迁移数据库。 我为用户名和电子邮件编写了测试,但是在执行以下命令之后: “捆绑执行佣金db:migrate” 我看到了一些错误,但我不明白自己做错了什么。

user.rb代码:

class User < ActiveRecord::Base
    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }
end

user_test.rb代码:

require 'test_helper'
class UserTest < ActiveSupport::TestCase
  def setup
    @user = User.new(name: "Example User", email: "user@example.com")
  end
  test "should be valid" do
    assert @user.valid?
  end
  test "name should be present" do
    @user.name = ""
    assert_not @user.valid?
  end
  test "email should be present" do
    @user.email = " "
    assert_not @user.valid?
  end
  test "name should not be too long" do
    @user.name = "a" * 51
    assert_not @user.valid?
  end
  test "email should not be too long" do
    @user.email = "a" * 244 + "@example.com"
    assert_not @user.valid?
  end
  test "email validation should accept valid addresses" do
    valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
                         first.last@foo.jp alice+bob@baz.cn]
    valid_addresses.each do |valid_address|
      @user.email = valid_address
      assert @user.valid?, "#{valid_address.inspect} should be valid"
    end
  end
  test "email validation should reject invalid addresses" do
    invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                           foo@bar_baz.com foo@bar+baz.com]
    invalid_addresses.each do |invalid_address|
      @user.email = invalid_address
      assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
    end
  end
  test "email addresses should be unique" do
    duplicate_user = @user.dup
    duplicate_user.email = @user.email.upcase
    @user.save
    assert_not duplicate_user.valid?
  end
end

第一个迁移文件:

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

第二个迁移文件:

class CreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

预期结果只是迁移,但实际结果是该错误:

rails db:migrate RAILS_ENV=test
== 20190323153324 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UniqueViolation: ERROR:  could not create unique index "index_users_on_email"
DETAIL:  Key (email)=(MyString) is duplicated.
: CREATE UNIQUE INDEX  "index_users_on_email" ON "users"  ("email")
/home/me/Workspace/blog/db/migrate/20190323153324_add_index_to_users_email.rb:3:in `change'
/home/me/Workspace/blog/bin/rails:9:in `require'
/home/me/Workspace/blog/bin/rails:9:in `<top (required)>'
/home/me/Workspace/blog/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  could not create unique index "index_users_on_email"
DETAIL:  Key (email)=(MyString) is duplicated.
: CREATE UNIQUE INDEX  "index_users_on_email" ON "users"  ("email")
/home/me/Workspace/blog/db/migrate/20190323153324_add_index_to_users_email.rb:3:in `change'
/home/me/Workspace/blog/bin/rails:9:in `require'
/home/me/Workspace/blog/bin/rails:9:in `<top (required)>'
/home/me/Workspace/blog/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
PG::UniqueViolation: ERROR:  could not create unique index "index_users_on_email"
DETAIL:  Key (email)=(MyString) is duplicated.
/home/me/Workspace/blog/db/migrate/20190323153324_add_index_to_users_email.rb:3:in `change'
/home/me/Workspace/blog/bin/rails:9:in `require'
/home/me/Workspace/blog/bin/rails:9:in `<top (required)>'
/home/me/Workspace/blog/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

0 个答案:

没有答案