RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 634247
Accepted
Sergei R
Sergei R
Asked:2020-03-02 01:41:44 +0000 UTC2020-03-02 01:41:44 +0000 UTC 2020-03-02 01:41:44 +0000 UTC

Rails SQLite3::ConstraintException:唯一约束失败:索引'index_users_on_reset_password_token'

  • 772

我用设计注册

为用户添加了用户名字段。对于授权,我使用电子邮件+密码。

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys:[:username])
  end
end

class User < ApplicationRecord
  validates :username, presence: true
  devise :database_authenticatable, 
     :registerable,
     :rememberable, 
     :trackable, 
     :validatable
end

ActiveRecord::Schema.define(version: 20170228132910) do
  create_table "users", force: :cascade do |t|
    t.string   "email",               default: "", null: false
    t.string   "encrypted_password",  default: "", null: false
    t.string   "username",            default: "", null: false
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",       default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                       null: false
    t.datetime "updated_at",                       null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index [nil], name: "index_users_on_reset_password_token", unique: true
  end

end

第一个用户正常注册,第二个我收到错误

ActiveRecord::RecordNotUnique in Devise::RegistrationsController#create SQLite3::ConstraintException: UNIQUE constraint failed: index 'index_users_on_reset_password_token': INSERT INTO "users" ("email", "encrypted_pa​​ssword", "username", "created_at", "updated_at" ) 值 (?, ?, ?, ?, ?)

index_users_on_reset_password_token 有什么问题?

添加迁移

class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      # Registrable
      t.string :username,           null: false, default: ""

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
  end
end
ruby-on-rails
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    user181100
    2020-03-02T05:33:46Z2020-03-02T05:33:46Z

    这已在 Rails 错误跟踪器 ( #27782 ) 中报告,并且是 SQLite 中的一个问题:

    SQLite 有一个(错误的)特性,即无法解析为表名或列名的双引号名称被视为字符串。这是一个非常早期的设计决定,早在 SQLite 流行起来并发现自己可以在地球上的所有设备上运行之前就做出了,旨在使 SQLite 与 MySQL 更加兼容,MySQL 在当时是世界上部署最广泛的数据库引擎. 我现在后悔那个选择,但我不能在不破坏向后兼容性的情况下撤消它。

    — D. Richard Hipp,SQLite 的创建者

    俄语:

    SQLite 有一个(hacky)特性:不能解析为表名或列名的双引号标识符被视为字符串。这是一个有意识的选择,早在 SQLite 起飞并进入世界各地的各种设备之前很久就做出了,它是为了提供与 MySQL 更好的兼容性,MySQL 是当时世界上最流行的 DBMS。现在我很后悔这个选择,但我不能在不破坏向后兼容性的情况下摆脱它。

    Rails 似乎决心在他们这边解决这个问题,但他们是否成功是一个悬而未决的问题。您不能只是从索引定义的列中获取和删除引号。这将完全修复这个错误,但它会增加对列名可以是什么的限制,甚至只是添加新的错误。


    在迁移中,您向不存在的列添加了唯一reset_password_token索引, . 由于上面的“特征”,结果是常量字符串( "reset_password_token") 上的功能(计算)索引。因此,在该索引下的表中不能有超过一行,因为插入另一行将需要添加一个已经存在于唯一索引中的值。美丽!

    现在的解决方案是进行新的迁移以删除此索引:

    remove_index :users, name: :index_users_on_reset_password_token
    
    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5