回报率:不能change_column Postgres里,罚款的MySQL(MySQL的发展,Postgres的在Heroku)回报率、change_column、Postgres、MySQL

2023-09-09 22:01:01 作者:映画゜ゝ

我有一个名为有奖列:

create_table :contests do |t|
  t.text :prize

最近我意识到,这将永远是一个整数,我想设置一个默认值:

I recently realized that this will always be an integer and I wanted to set a default value:

def change
  change_column :contests, :prize, :integer, :default => 200

这工作正常在我的本地机器上,在这里我使用的MySQL数据库。然而,当我把我的生产基地(其中托管在Heroku上,这给了我一个Postgres数据库)我得到以下错误:

This works fine on my local machine, where I am using MySQL DB. However, when I push to my production site (which is hosted on Heroku, which gives me a Postgres DB) I get the following error:

PGError: ERROR:  column "prize" cannot be cast to type "pg_catalog.int4"
: ALTER TABLE "contests" ALTER COLUMN "prize" TYPE integer

在这篇文章: http://www.postgresonline.com/periodical.php? i_id = 3 他们讨论了使用using来解决这个问题。但我不知道我能做到这一点,这是否适合我想要做的事情。

In this article: http://www.postgresonline.com/periodical.php?i_id=3 they discuss the use of the USING to solve this problem. But I don't know how I can do that, and whether this is appropriate for what I'm trying to do.

任何深入了解弄清这一点是非常AP preciated。

Any insight into figuring this out would be very much appreciated.

谢谢! 林戈

推荐答案

首先,你应该使用相同的数据库在两个环境中,以prevent这样的惊喜。

First you should use the same DB in both environments to prevent this kind of surprises.

要运行原始的SQL迁移中看到这样的例子 HTTP:/ /guides.rubyonrails.org/migrations.html#using-the-up-down-methods

To run raw sql in migrations see this example http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods