我正在关注rails教程。我在上。 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
如果相关,我可以发布任何其他文件。在此先感谢您的帮助!
答案 0 :(得分:22)
问题是我在迁移之前在数据库中使用了相同的电子邮件。
db:reset
解决了所有问题
答案 1 :(得分:3)
我遇到了一些不同的原因,这是解决方案。
对我来说错误是
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 未填充引起的,因此在具有唯一键约束的列中存在重复值。
测试/装置/ 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
答案 2 :(得分:2)
如果你不能reset:db
你应该尝试手动删除它,请转到db文件夹并删除&#34; development.sqlite3
&#34;和#34; test.sqlite3
&#34;档
然后运行
rails db:migrate
和bin/rails db:migrate RAILS_ENV=TEST
为我工作......
答案 3 :(得分:1)
如果您在运行测试时遇到这种情况:
确保没有用户设备,除非您明确定义具有唯一属性的用户。
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
这会打乱你的测试。
答案 4 :(得分:0)
只需使用rake db:test:prepare
,它将复制开发数据库以进行测试,因此您无需运行迁移。