Rails 4 Ruby 2.00现有用户模型上的Devise迁移失败 [英] Rails 4 Ruby 2.00 Devise migration on existing User Model fails

查看:153
本文介绍了Rails 4 Ruby 2.00现有用户模型上的Devise迁移失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将Devise添加到已经实现的Omniauth的Enki应用程序和现有的用户模型中。在跟随github的Devise教程之后,尝试几乎每个可能的解决方案发布在这里或在Devise wiki,我仍然无法获得迁移运行。我得到这个错误,我运行rake db:migrate时得到。我知道我需要注释掉电子邮件列(它存在于用户模型上)。我试图给出任何一个抛出已经存在的错误,但是也没有。

I am trying to add Devise to an Enki based app with Omniauth already implemented and an existing User Model. After following the Devise tutorial on github and trying almost every possible solution posted here or in the Devise wiki, I still cannot get the migration to run.I get this error I get when running rake db:migrate. I am aware that I need to comment out the email column(it exists on the User model). I tried to comment out whichever throwing the 'already exist' error, but that did not work either.

$ rake db:migrate
==  AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::DuplicateColumn: ERROR:  column "encrypted_password" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "encrypted_password" character varying(255) DEFAULT '' NOT NULL/Users/josecfernandezortiz/.rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'

这是当前的架构:

  create_table "users", force: true do |t|
    t.string   "name",           null: false
    t.string   "email",          null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "provider"
    t.string   "uid"
    t.string   "role"
    t.datetime "last_logged_in"
  end

这是Devise生成的迁移:

And this is the migration generated by Devise:

class AddDeviseToUsers < ActiveRecord::Migration
  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
    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

我也试图注释掉 t.string:encrypted_pa​​ssword,:null => false,:default =>
,结果相同。
谢谢!

I also tried to comment out t.string :encrypted_password, :null => false, :default => "" , same result. Thank you!

推荐答案

您的 schema.rb 将仅包含与通过该特定项目的迁移所做的更改相关的模式信息。所以如果你添加了一列OUTSIDE的迁移,架构就不会反映出来。我会冒昧地猜测,您可能已经将Devise或者Devise所使用的某些/全部这些列添加到了先前的项目中,同名的表上。

Your schema.rb file will ONLY contain schema information related to changes made through the migrations of that particular project. So if you added a column OUTSIDE of migrations, schema will not reflect that. I would venture to guess that you might have added Devise OR some/all of those columns used by Devise in a prior project, on a table with the same name.

可以执行以下任何操作:

You could do any of the following:

1)检查用户,并确认是否是这种情况,如果是
a)删除数据库并重新创建(我认为可能还有其他问题/表/列你不期望)
b)删除列

1) Inspect the User, and confirm if that's the case, and if it is either a) Drop the DB and recreate (I assume there might be other issues/tables/columns you aren't expecting) b) Remove the columns

您可以删除并重新创建如下: / p>

You can drop and recreate as follows:

rake db:drop db:create db:schema:load

这篇关于Rails 4 Ruby 2.00现有用户模型上的Devise迁移失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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