Rails 教程:SQLite3::ConstraintException:唯一约束失败:users.email

Rails Tutorial: SQLite3::ConstraintException: UNIQUE constraint failed: users.email

我正在学习 rails 教程。我在 ch。 6 并且我在使用 SQLite3 时遇到了一个奇怪的错误(为了记录,我使用的是 sqlite 版本 1.3.10,而教程使用的是 1.3.9)

当我 运行 rake db:migrate 时我没有收到错误,但是当我 运行 测试环境的迁移时,这是我得到的:

$ bundle exec rake test:models

rake aborted!
ActiveRecord::PendingMigrationError:

Migrations are pending. To resolve this issue, run:

    bin/rake db:migrate RAILS_ENV=test

sample_app/test/test_helper.rb:3:in `<top (required)>'
sample_app/test/models/user_test.rb:1:in `require'
sample_app/test/models/user_test.rb:1:in `<top (required)>'
Tasks: TOP => test:models
(See full trace by running task with --trace)


$ bundle exec rake db:migrate RAILS_ENV=test

== 20150628011937 AddIndexToUsersEmail: migrating ===========================
==
    -- add_index(:users, :email, {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:


SQLite3::ConstraintException: UNIQUE constraint failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constrain
t failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users"
("email")
sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
SQLite3::ConstraintException: UNIQUE constraint failed: users.email
sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

这是我的 user.rb 模型:

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  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

这是我最近的迁移

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

如果相关,我可以post任何其他文件。在此先感谢您的帮助!

问题是我在数据库中有用户在迁移之前使用相同的电子邮件。

db:reset 解决了一切

如果你不能reset:db你应该尝试手动删除它去db文件夹并删除“development.sqlite3”和“test.sqlite3”文件 然后 运行 rails db:migratebin/rails db:migrate RAILS_ENV=TEST

无论如何对我有用...

我遇到它的原因有点不同,这里是解决方案。

对我来说错误是

Minitest::UnexpectedError: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.email: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2018-05-09 18:23:59.827561', '2018-05-09 18:23:59.827561', 298486374)

注意 INTO "users" ("created_at", "updated_at", "id")。是由于test/fixtures/users.yml没有被填充,所以在有唯一键约束的列中出现了重复值。

test/fixtures/users.yml:

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

只需使用 rake db:test:prepare,它将复制开发数据库进行测试,因此您不需要 运行 迁移。

如果您在 运行 测试时遇到这种情况:

确保没有用户固定装置,除非您明确定义那些具有独特属性的用户。

test/fixtures/users.yml

生成模型时,例如:

rails g model User

Rails 自动创建该文件,它看起来像:

# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

这可能会打乱您的测试。