推送到Heroku时Rails迁移错误w / Postgres

时间:2022-09-06 00:16:26

I'm trying to perform the following up migration to change the column "number" in the "tweet" model's table

我正在尝试执行以下向上迁移以更改“tweet”模型表中的“number”列

class ChangeDataTypeForTweetsNumber < ActiveRecord::Migration
  def up
    change_column :tweets do |t|
      t.change :number, :integer
    end
  end

  def down
    change_table :tweets do |t|
      t.change :number, :string
    end
  end
end

Upon performing the the following up migration to heroku....

执行以下向上迁移到heroku ....

heroku rake db:migrate:up VERSION=20120925211232

I get the following error

我收到以下错误

    PG::Error: ERROR:  column "number" cannot be cast to type integer
: ALTER TABLE "tweets" ALTER COLUMN "number" TYPE integer

Any thoughts you have would be very much appreciated.

任何想法都会非常感激。

Thanks everyone.

感谢大家。

2 个解决方案

#1


31  

From the fine manual:

从精细手册:

[ALTER TABLE ... ALTER COLUMN ...]
The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.

[ALTER TABLE ... ALTER COLUMN ...]可选的USING子句指定如何从旧的计算新列值;如果省略,则默认转换与从旧数据类型转换为new的赋值相同。如果没有从旧类型到新类型的隐式或赋值转换,则必须提供USING子句。

There is no implicit conversion from varchar to int in PostgreSQL so it complains that column "number" cannot be cast to type integer and the ALTER TABLE fails. You need to tell PostgreSQL how to convert the old strings to numbers to match the new column type and that means that you need to get a USING clause into your ALTER TABLE. I don't know of any way to make Rails do that for you but you can do it by hand easily enough:

在PostgreSQL中没有从varchar到int的隐式转换,所以它抱怨列“number”不能转换为整数类型而ALTER TABLE失败。您需要告诉PostgreSQL如何将旧字符串转换为数字以匹配新列类型,这意味着您需要在ALTER TABLE中获取USING子句。我不知道有什么方法可以让Rails为你做到这一点,但你可以轻松地手工完成:

def up
  connection.execute(%q{
    alter table tweets
    alter column number
    type integer using cast(number as integer)
  })
end

You'll want to watch out for values that can't be cast to integers, PostgreSQL will let you know if there are problems and you'll have to fix them before the migration will succeed.

您需要注意无法转换为整数的值,PostgreSQL会告诉您是否存在问题,并且您必须在迁移成功之前修复它们。

Your existing down-migration should be fine, converting integer to varchar should be handled automatically.

您现有的向下迁移应该没问题,应该自动处理将整数转换为varchar。

#2


45  

Same as above but a little bit more concise:

与上面相同,但更简洁一点:

change_column :yourtable, :column_to_change, 'integer USING CAST("column_to_change" AS integer)'

#1


31  

From the fine manual:

从精细手册:

[ALTER TABLE ... ALTER COLUMN ...]
The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.

[ALTER TABLE ... ALTER COLUMN ...]可选的USING子句指定如何从旧的计算新列值;如果省略,则默认转换与从旧数据类型转换为new的赋值相同。如果没有从旧类型到新类型的隐式或赋值转换,则必须提供USING子句。

There is no implicit conversion from varchar to int in PostgreSQL so it complains that column "number" cannot be cast to type integer and the ALTER TABLE fails. You need to tell PostgreSQL how to convert the old strings to numbers to match the new column type and that means that you need to get a USING clause into your ALTER TABLE. I don't know of any way to make Rails do that for you but you can do it by hand easily enough:

在PostgreSQL中没有从varchar到int的隐式转换,所以它抱怨列“number”不能转换为整数类型而ALTER TABLE失败。您需要告诉PostgreSQL如何将旧字符串转换为数字以匹配新列类型,这意味着您需要在ALTER TABLE中获取USING子句。我不知道有什么方法可以让Rails为你做到这一点,但你可以轻松地手工完成:

def up
  connection.execute(%q{
    alter table tweets
    alter column number
    type integer using cast(number as integer)
  })
end

You'll want to watch out for values that can't be cast to integers, PostgreSQL will let you know if there are problems and you'll have to fix them before the migration will succeed.

您需要注意无法转换为整数的值,PostgreSQL会告诉您是否存在问题,并且您必须在迁移成功之前修复它们。

Your existing down-migration should be fine, converting integer to varchar should be handled automatically.

您现有的向下迁移应该没问题,应该自动处理将整数转换为varchar。

#2


45  

Same as above but a little bit more concise:

与上面相同,但更简洁一点:

change_column :yourtable, :column_to_change, 'integer USING CAST("column_to_change" AS integer)'