【问题标题】:Error when using gem 'devise' for an already existing model对已存在的模型使用 gem 'devise' 时出错
【发布时间】:2019-03-31 09:44:54
【问题描述】:

我刚刚尝试添加 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(:users) 导轨中止! StandardError:发生错误,所有后续迁移都已取消:

Mysql2::Error: Duplicate column name 'email': ALTER TABLE users ADD email varchar(255) DEFAULT '' NOT NULL

原因: ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name 'email': ALTER TABLE users ADD email varchar(255) DEFAULT '' NOT NULL

我检查了人们给出的问题和解决方案,但没有一个能帮助我解决这个问题(我是新来的)

【问题讨论】:

    标签: ruby-on-rails rubygems


    【解决方案1】:

    阻止change_table 向现有表users 添加列。在change_table 中,您尝试添加列email,但该列已存在于表users 中。此列是在您的第一次迁移中创建的。

    您应该在设计迁移中注释以下字符串:

     t.string :email,              null: false, default: ""
    

    并在change_table (NOT IN BLOCK) 方法之后在设计迁移中添加跟随字符串:

    change_column :users, :email, :string, null: false, default: ""
    

    【讨论】:

    • 按照你说的做后,我发现问题仍然存在,但其他属性如 encypted_pa​​sswordsign_in_count 等...跨度>
    • @MinhTâmLương 看着你db/schema.rb。你看到users 的列了吗?
    • 好的,根据你的想法,我实际上删除了块并更改了每个属性作为你给我的电子邮件样本,我可以迁移。所以我觉得现在去也不错。谢谢!!!!!!!!!
    • 是的,我有用户列
    • @MinhTâmLương 这意味着您的表已经存在此列,您尝试第二次为创建列执行迁移。如果您使用开发环境并且不需要数据库中的数据,我建议您删除 db 并重新运行所有迁移。
    猜你喜欢
    • 2011-08-26
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    相关资源
    最近更新 更多