RoR:无法在 postgres 中更改列,在 MySQL 中很好(用于开发的 MySQL,Heroku 上的 Postgres) [英] RoR: Cannot change_column in postgres, fine in MySQL (MySQL for development, Postgres on Heroku)

查看:19
本文介绍了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

这篇关于RoR:无法在 postgres 中更改列,在 MySQL 中很好(用于开发的 MySQL,Heroku 上的 Postgres)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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