加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在现有的模型上设计移植

发布时间:2020-12-16 22:38:07 所属栏目:百科 来源:网络整理
导读:我从Authlogic迁移到Devise. 更新: devise的迁移尝试重新创建表用户,所以我改变了,你可以在下面看到create_table到change_table和drop table到最后删除我添加的内容 问题是当我运行rake我得到一个错误. 这是在运行耙子时得到的错误. == DeviseCreateUsers:
我从Authlogic迁移到Devise.

更新:

devise的迁移尝试重新创建表用户,所以我改变了,你可以在下面看到create_table到change_table和drop table到最后删除我添加的内容

问题是当我运行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,: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获得了这个.

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"

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

谢谢!

解决方法

您所获得的错误是因为您尝试重新创建已有的电子邮件字段.电子邮件字段在设计助手t.database_authenticatable中创建.您可以将旧用户表与新系统一起使用,但不需要包含t.database_authenticatable,您只需要重命名旧的字段名称即可.查看 Documentation for Devise,您可以看到database_authenticatable只创建三个字段:email,encrypted_pa??ssword和password_salt.它们与您在authlogic用户表中已有的电子邮件,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读