如何在Heroku中更改列类型? [英] How do I change column type in Heroku?

查看:140
本文介绍了如何在Heroku中更改列类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图耙到db:migrations到我的heorku实例,我得到一个错误。常见问题描述我的错误如下:


$ b :PGError:ERROR:column
verified_at不能转换为类型
date



原因:PostgreSQL不知道如何
将该表中的所有行转换为
指定的类型。很可能意味着

的那一列中有一个整数或字符串。



解决方案:检查您的记录和
确认他们可以转换为
新类型。有时更容易
只是避免使用change_column,
重命名/创建一个新列


如何立即更改此迁移。这是我有的问题。对于我的联系人表,我创建了以下:

  t.string:date_entered 



在稍后的迁移中,我执行以下操作:

  change_column:contacts,:date_entered,:date 

此change_column似乎有问题。 p>

我应该...手动改变迁移吗?有没有办法我可以清理我的表中的数据(我不知道Heroku会认可表中的数据,因为我做一个耙子)。



我显然需要改变这个值,它在我的应用程序使用。非常感谢。



这是我想要的...想法?

  def self.up 
#change_column:contacts,:date_entered,:date
#在postgres中失败,所以尝试相同的结果

rename_column:contacts,:date_entered,:date_entered_old
add_column:contacts,:date_entered,:date
remove_column:contacts,:date_entered_old
end

def self.down
add_column:contacts,:date_entered_old
remove_column:contacts,:date_entered
rename_column:contacts,:date_entered_old,:date_entered
end
执行以下操作:


    li>重命名列A
  1. 创建新列B作为日期

  2. 将数据从A移动到B

  3. 删除A

换句话说,

  def self.up 
rename_column:contacts,:date_entered,:date_entered_string
add_column:contacts,:date_entered,:date

Contact.reset_column_information
Contact.find_each {| c | c.update_attribute(:date_entered,c.date_entered_string)}
remove_column:contacts,:date_entered_string
end


I am trying to rake the db:migrations into my heorku instance and I get an error. The FAQ described my error as below:

Cannot change column type

Example: PGError: ERROR: column "verified_at" cannot be cast to type "date"

Cause: PostgreSQL doesn’t know how to cast all the rows in that table to the specified type. Most likely it means you have an integer or a string in that column.

Solution: Inspect your records and make sure they can be converted to the new type. Sometimes it’s easier to just avoid using change_column, renaming/creating a new column instead.

How do I change this migration now. This is the problem that I have. For my Contacts table, I created the following:

  t.string :date_entered

In a later migration, I do the following:

 change_column :contacts, :date_entered, :date

This change_column appears to be the problem.

Should I...change by hand that migration? Is there a way I can clean the data in my tables (I didn't know Heroku would recognize the data in the table because I'm doing a rake).

I obviously need to change this value and it is used throughout my application. Thanks.

This is what I am trying...thoughts?

def self.up
  #change_column :contacts, :date_entered, :date
  #this fails in postgres, so trying the same outcome 

  rename_column :contacts, :date_entered, :date_entered_old
  add_column :contacts, :date_entered, :date
  remove_column :contacts, :date_entered_old
end

def self.down
  add_column :contacts, :date_entered_old
  remove_column :contacts, :date_entered
  rename_column :contacts, :date_entered_old, :date_entered
end

解决方案

Do the following:

  1. rename the column A
  2. create the new column B as date
  3. move the data from A to B
  4. remove A

In other words

def self.up
  rename_column :contacts, :date_entered, :date_entered_string
  add_column :contacts, :date_entered, :date

  Contact.reset_column_information
  Contact.find_each { |c| c.update_attribute(:date_entered, c.date_entered_string) } 
  remove_column :contacts, :date_entered_string
end

这篇关于如何在Heroku中更改列类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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