RoR:无法在 postgres 中更改列,在 MySQL 中很好(用于开发的 MySQL,Heroku 上的 Postgres)

编程入门 行业动态 更新时间:2024-10-27 02:31:31
本文介绍了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

在本文中:www.postgresonline/periodical.php?i_id=3 他们讨论了使用USING来解决这个问题.但我不知道如何才能做到这一点,也不知道这是否适合我正在尝试做的事情.

In this article: www.postgresonline/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,请参见此示例 guides.rubyonrails/migrations.html#using-the-up-down-methods

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

更多推荐

RoR:无法在 postgres 中更改列,在 MySQL 中很好(用于开发的 MySQL,Heroku 上的 Postgres)

本文发布于:2023-10-16 05:16:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:很好   postgres   RoR   MySQL   Postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!