在现有模型上设计迁移

Mar*_*tin 9 ruby-on-rails

我正在从Authlogic迁移到Devise.

更新:

设计的迁移尝试重新创建表用户,所以我改变了,你可以在下面看到create_table到change_table并删除表的末尾以删除我添加的内容

问题是当我运行rake时我得到一个错误.

这是我在运行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
Run Code Online (Sandbox Code Playgroud)

这是迁移

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
Run Code Online (Sandbox Code Playgroud)

在我的schema.rb中,我已经从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"
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

jam*_*raa 22

您得到的错误是因为您正在尝试重新创建email已有的字段.该email字段在设计助手中创建t.database_authenticatable.您可以将旧用户表与新系统一起使用,但不需要包含t.database_authenticatable,只需重命名旧字段名称即可.看在了设计文档,你可以看到database_authenticatable刚刚创建三个领域:电子邮件,encrypted_pa​​ssword和password_salt.它们与您在authlogic用户表中已有的email,crypted_pa​​ssword和password_salt相同,因此您可以像这样使用change_table:

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
Run Code Online (Sandbox Code Playgroud)

  • 您能否为您的解决方案回复或更新原始问题? (3认同)