Ruby on Rails 5.0 update with migrating user table conflicts

After upgrading to Rails 5.0 from 4.2, I get 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 following two stack overflow messages:

PGError: ERROR: email column user relationship already exists

Develop an existing model migration

My question is the best way to resolve a database conflict that occurs with a user table?

Is it better to edit an existing migration file, for example, AddDeviseToUsers 20160703164716 migration, or is it recommended to perform a new migration?

What is the team to create a new migration and a better name for this migration that prevents the 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 do you take this code and make a new migration?

After studying migration, I performed the following migration:

 rails g migration change_data_type_for_users 

Creating a new migration that I edited, in accordance with the fact that I understand the above error, suggesting a fix in the user fields. New transition code:

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

After starting the db: migrate rails, I get the same error. What am I doing wrong? Should I roll back or change a new migration?

Since then, I found the article "Stack Overflow". ( Rails 4 Ruby 2.00 Eliminate the migration of an existing user model ) Is this the right way? Will deleting a database delete all db data?

Another find here is that if running rake db: reset will destroy the data in my database. Does this recreate the database? It is unclear, and from the next message it seems that it will be very dangerous if all the data is destroyed. We just want to fix one field in one table.

( The difference between rake db: migrate db: reset and db: schema: load )

I'm really trying to answer my own question, so maybe this model has some difference, which is in addition to the user model, which, apparently, was 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 
+1
source share
1 answer

in your case, remember that you are trying to change the column that exists in your database, the way you do it, as if you are creating a new one.

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

you can change this line to

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

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

+1
source

All Articles