Devise migration on existing model

后端 未结 4 1267
北海茫月
北海茫月 2020-12-08 17:47

I\'m migrating from Authlogic to Devise.

UPDATED:

The migration of devise tries to re-create the table users, so i changed as you can see below the create_

相关标签:
4条回答
  • 2020-12-08 18:02

    Instead of changing the create_table to change_table, you can just add the following line just before create_table:

    rename_table :users, :old_users_authlogic
    

    Then, right after the create_table:

    say_with_time 'Migrating users from Authlogic to Devise...' do
      execute "INSERT INTO users (id, email, ...) SELECT id, email FROM old_users_authlogic"
    end
    

    If you are using indexes with referential integrity, don't forget to update them to the new table, as the rename_table will make them point to old_users_authlogic.

    0 讨论(0)
  • 2020-12-08 18:19

    I solved this by deleting Devise migration file but not removing it from Sublime. I then did a rails g migration remove_email_from_foo, thus removing email attribute. I then cancelled the Devise delete migration file which bought the migration file back in & ran a rake db:migrate. Devise attributes saved to model

    0 讨论(0)
  • 2020-12-08 18:22

    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
    
    0 讨论(0)
  • 2020-12-08 18:25
    add_column(:users, :database_authenticatable, {:null=>false})
    

    You are not providing information for how this field is to be setup. It needs to be a string I think so you want to pass that information.

    add_column :users, :database_authenticatable, :string,  :null=>false
    

    More information about the error you are getting:

    undefined method `to_sym'
    

    That error for migrations is really frustrating because it lacks content, but what it means is that you got your migration in the wrong order.

    For example - if you add a boolean file to the use model:

     add_column :users, :user_is_a_jackass, :boolean, :default => false
    

    That will migrate ok. But if you do it like this:

    add_column :users, :user_is_a_jackass, :default => false
    

    You will get the same error because you are not telling the migration what field type it should be.

    0 讨论(0)
提交回复
热议问题