RoR: Cannot change_column in postgres, fine in MySQL (MySQL for development, Postgres on Heroku)(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RoR:无法在 postgres 中更改列,在 MySQL 中很好(用于开发的 MySQL,Heroku 上的 Postgres)


基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01