Rails 教程:SQLite3::ConstraintException:唯一约束失败:users.email [英] Rails Tutorial: SQLite3::ConstraintException: UNIQUE constraint failed: users.email

查看:46
本文介绍了Rails 教程:SQLite3::ConstraintException:唯一约束失败:users.email的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Rails 教程.我在 ch.6 并且我在使用 SQLite3 时遇到了一个奇怪的错误(为了记录,我使用的是 SQLite 版本 1.3.10,而本教程使用的是 1.3.9)

I'm following along with the rails tutorial. I'm on ch. 6 and I'm getting a strange error with SQLite3 (for the record, I'm using sqlite version 1.3.10 and the tutorial uses 1.3.9)

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

I don't get an error when I run rake db:migrate, but when I run the migration for the test environment, here's what I get:

$ 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 模型:

Here is my user.rb model:

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

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

I can post any other files if those are relevant. Thanks in advance for the help!

推荐答案

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

The problem was that I had users in the database with the same email before the migration.

db:reset 解决了一切

这篇关于Rails 教程:SQLite3::ConstraintException:唯一约束失败:users.email的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆