Ruby on Rails 5.0升级,迁移用户表冲突 [英] Ruby on Rails 5.0 upgrade with migrating users table conflicts

查看:317
本文介绍了Ruby on Rails 5.0升级,迁移用户表冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从4.2升级到Rails 5.0后,得到以下错误:

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'

读取以下两个Stack Overflow帖子后:

After reading the two following Stack Overflow posts:

PGError:错误:关系用户的列电子邮件已存在

在现有模型上设计迁移

我的问题是,解决数据库冲突的最佳方法是用户表?

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

最好是编辑现有的迁移文件,例如20160703164716 AddDeviseToUsers迁移,或者建议进行新的迁移?

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

进行新迁移的命令是什么以及此迁移的最佳名称,以防止我的开发和基于Heroku的生产环境中的数据库冲突?

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

运行rails db:migrate后,同样的错误。我究竟做错了什么?我现在应该回滚或编辑新的迁移吗?

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

我发现这个Stack Overflow文章。 ( Rails 4 Ruby 2.00 Devise migration on现有用户模型失败
这是正确的路径吗?将删除数据库删除所有的数据库数据?

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?

另一个发现在这里导致相信如果执行rake db:reset会破坏我的数据库中的数据。这样做是否重新创建数据库?它不清楚,从后面的帖子似乎是如果所有的数据被破坏将是非常有害的。我们只想修正一个表中的一个字段。

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.

rake db:migrate db:reset和db:schema:load

我真的想回答我自己的问题,是似乎由ActiveAdmin创建的用户模型的补充:

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 => ""

您可以将该行更改为

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

您知道t是您的用户表格

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

这篇关于Ruby on Rails 5.0升级,迁移用户表冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆