对已存在的模型使用 gem 'devise' 时出错
Error when using gem 'devise' for an already existing model
我刚刚尝试添加 gem 'devise' 但遇到了一些错误。
但在此之前我创建了一个这样的模型用户:
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :name
t.string :email
t.integer :role
t.string :password_digest
t.timestamps
end
end
end
然后我 运行 命令 'rails generate devise User' 它创建了一个迁移
add_devise_to_user:
class AddDeviseToUsers < ActiveRecord::Migration[5.2]
def self.up
change_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## 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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if
lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email
or :both
# t.datetime :locked_at
# Uncomment below if timestamps were not included in your
original model.
t.timestamps null: false
end
# add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
def self.down
# By default, we don't want to make any assumption about how to
roll back a migration when your
# model already existed. Please edit below which fields you
would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
每当我尝试 运行 rails db:migrate 时,终端都会向我显示这些错误
== 20190331081250 AddDeviseToUsers: migrating =================================
-- change_table(:用户)
rails中止!
StandardError:发生错误,所有后来的迁移都已取消:
Mysql2::Error: 列名重复 'email': ALTER TABLE users
ADD email
varchar(255) DEFAULT '' NOT NULL
原因:
ActiveRecord::StatementInvalid: Mysql2::Error: 列名重复 'email': ALTER TABLE users
ADD email
varchar(255) DEFAULT '' NOT NULL
我已经查看了人们给出的问题和解决方案,但是 none 帮助我解决了这个问题(我是新来的)
在 change_table
中阻止将列添加到现有 table users
。在 change_table
中,您尝试添加列 email
,但此列已存在于 table users
中。此列是在您的第一次迁移中创建的。
您应该在设计迁移中对字符串进行评论:
t.string :email, null: false, default: ""
并在 change_table
(NOT IN BLOCK)方法后的设计迁移中添加以下字符串:
change_column :users, :email, :string, null: false, default: ""
我刚刚尝试添加 gem 'devise' 但遇到了一些错误。
但在此之前我创建了一个这样的模型用户:
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :name
t.string :email
t.integer :role
t.string :password_digest
t.timestamps
end
end
end
然后我 运行 命令 'rails generate devise User' 它创建了一个迁移 add_devise_to_user:
class AddDeviseToUsers < ActiveRecord::Migration[5.2]
def self.up
change_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## 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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if
lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email
or :both
# t.datetime :locked_at
# Uncomment below if timestamps were not included in your
original model.
t.timestamps null: false
end
# add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
def self.down
# By default, we don't want to make any assumption about how to
roll back a migration when your
# model already existed. Please edit below which fields you
would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
每当我尝试 运行 rails db:migrate 时,终端都会向我显示这些错误
== 20190331081250 AddDeviseToUsers: migrating =================================
-- change_table(:用户) rails中止! StandardError:发生错误,所有后来的迁移都已取消:
Mysql2::Error: 列名重复 'email': ALTER TABLE users
ADD email
varchar(255) DEFAULT '' NOT NULL
原因:
ActiveRecord::StatementInvalid: Mysql2::Error: 列名重复 'email': ALTER TABLE users
ADD email
varchar(255) DEFAULT '' NOT NULL
我已经查看了人们给出的问题和解决方案,但是 none 帮助我解决了这个问题(我是新来的)
在 change_table
中阻止将列添加到现有 table users
。在 change_table
中,您尝试添加列 email
,但此列已存在于 table users
中。此列是在您的第一次迁移中创建的。
您应该在设计迁移中对字符串进行评论:
t.string :email, null: false, default: ""
并在 change_table
(NOT IN BLOCK)方法后的设计迁移中添加以下字符串:
change_column :users, :email, :string, null: false, default: ""