Ben*_*hol 11 ruby-on-rails railstutorial.org
我正在跟随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)
Run Code Online (Sandbox Code Playgroud)
这是我的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
Run Code Online (Sandbox Code Playgroud)
这是我最近的迁移
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
Run Code Online (Sandbox Code Playgroud)
如果相关,我可以发布任何其他文件.在此先感谢您的帮助!
我出于不同的原因经历了它,这是解决方案。
对我来说,错误是
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)
Run Code Online (Sandbox Code Playgroud)
请注意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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10129 次 |
| 最近记录: |