RoR:无法在 postgres 中更改列,在 MySQL 中很好(用于开发的 MySQL,Heroku 上的 Postgres)
我有一个名为奖品"的专栏:
I had a column called "prize":
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.
非常感谢您对解决此问题的任何见解.
Any insight into figuring this out would be very much appreciated.
谢谢!林戈
推荐答案
首先,您应该在两种环境中使用相同的 DB,以防止出现此类意外.
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
相关文章