Ruby on Rails 5.0 upgrade with migrating users table conflicts

After upgrading to Rails 5.0 from 4.2 I am getting the following error:

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

PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
/Users/my_username/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/Users/my_username/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'

After reading the two following Stack Overflow posts:

PGError: ERROR: column “email” of relation “users” already exists

Devise migration on existing model

My question is which is the best way to solve the database conflict occurring with the users table?

Is it best to edit an existing migration file like the 20160703164716 AddDeviseToUsers migration or is it advisable to make a new migration?

What is the command to make a new migration and the best name for this migration that prevents database conflict in my development and Heroku based production environment?

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

Above is the suggested code suggested from the second post.

How would you take this code and make a new migration out of it?

After researching migrations I have made the following migration:

rails g migration change_data_type_for_users

Generating a new migration that I edited according to what I understand the error above to be suggesting to fix in the users fields. The new migration code:

class ChangeDataTypeForUsers < ActiveRecord::Migration[5.0]
  def change
    change_table(:users) do |t|
      t.string :email,              :null => false, :default => ""
  end
end

After running rails db:migrate I receive the same error. What am I doing wrong? Should I now rollback or edit the new migration?

I have since found this Stack Overflow article. (Rails 4 Ruby 2.00 Devise migration on existing User Model fails) Is this the right path to take? Will dropping the database delete all the db data?

Another find here leads to believe if executing rake db:reset will destroy the data in my database. Does doing this recreate the database? It is not clear and from the following post seems like it would be very damaging if all the data is destroyed. We just want to fix one field in one table.

(Difference between rake db:migrate db:reset and db:schema:load)

I am really trying to answer my own question so perhaps this model makes some difference that is addition to the user model that seems to have been created by ActiveAdmin:

class AdminUser < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable
end

in your case remember that you are trying to change a column that exist in your database, in the way that you are doing this is like you are creating a new one.

t.string :email, :null => false, :default => ""

you can change that line to

t.change :email, :string, :null => false, :default => ""

inside of your block, knowing that "t" is your users table

链接地址: http://www.djcxy.com/p/56904.html

上一篇: Rails:开发和生产之间的迁移和数据库差异

下一篇: Ruby on Rails 5.0与迁移用户表冲突升级