在现有模型上设计迁移 [英] Devise migration on existing model

查看:21
本文介绍了在现有模型上设计迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Authlogic 迁移到 Devise.

I'm migrating from Authlogic to Devise.

更新:

设计的迁移尝试重新创建表用户,因此我将create_table下方更改为change_table并在末尾删除表以删除我添加的内容

The migration of devise tries to re-create the table users, so i changed as you can see below the create_table to change_table and drop table at the end to remove what i add

问题是当我运行 rake 时出现错误.

The problem is when I run rake i get an error .

这是我在运行 rake 时遇到的错误.

This is the error I get when running rake.

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

SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL

这是迁移

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    change_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      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
    remove_column :users, :database_authenticatable
    remove_column :users, :recoverable
    remove_column :users, :rememberable
    remove_column :users, :trackable
    remove_index :users, :email
    remove_index :users, :reset_password_token
  end
end

在我的 schema.rb 中,我已经有了来自 Authlogic 的这个.

In my schema.rb i already have this from Authlogic.

  create_table "users", :force => true do |t|
    t.string    "username"
    t.string    "email"
    t.string    "crypted_password"
    t.string    "password_salt"
    t.string    "persistence_token"

我认为它看到了某种我无法意识到如何避免与那些设计助手的冲突

I think it sees some kind of conflict that i'm not able to realize how to avoid with those devise helpers

谢谢!

推荐答案

您收到的错误是因为您正在尝试重新创建您已有的 email 字段.email 字段是在设计助手 t.database_authenticable 中创建的.您可以在新系统中使用旧用户表,但不需要包含 t.database_authenticable,您只需要重命名旧字段名称.查看Devise 文档,您可以看到database_authenticable 只创建三个字段:email、encrypted_pa​​ssword 和 password_salt.它们与您的 authlogic 用户表中已有的 email、crypted_pa​​ssword 和 password_salt 相同,因此您可以像这样使用 change_table:

The error that you're getting is because you are trying to recreate the email field which you already have. The email field is created in the devise helper t.database_authenticatable. You can use your old user table with the new system, but you don't need to include t.database_authenticatable, you just need to rename your old field names. Looking in the Documentation for Devise, you can see that database_authenticatable just creates three fields: email, encrypted_password and password_salt. They are the same as the email, crypted_password, and password_salt that you already have in your authlogic user table, so you can use change_table like so:

def self.up
  change_table(:users) do |t|
    t.recoverable
    t.trackable
    # rememberable uses remember_token, but this field is different
    t.rename :remember_token_expires_at, :remember_created_at
    # these fields are named differently in devise
    t.rename :crypted_password, :encrypted_password
  end
end

这篇关于在现有模型上设计迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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