我用设计注册
为用户添加了用户名字段。对于授权,我使用电子邮件+密码。
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_password", "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
这已在 Rails 错误跟踪器 ( #27782 ) 中报告,并且是 SQLite 中的一个问题:
俄语:
Rails 似乎决心在他们这边解决这个问题,但他们是否成功是一个悬而未决的问题。您不能只是从索引定义的列中获取和删除引号。这将完全修复这个错误,但它会增加对列名可以是什么的限制,甚至只是添加新的错误。
在迁移中,您向不存在的列添加了唯一
reset_password_token索引, . 由于上面的“特征”,结果是常量字符串("reset_password_token") 上的功能(计算)索引。因此,在该索引下的表中不能有超过一行,因为插入另一行将需要添加一个已经存在于唯一索引中的值。美丽!现在的解决方案是进行新的迁移以删除此索引: