【问题标题】:Error on Heroku trying to change DB column TypeHeroku 尝试更改数据库列类型时出错
【发布时间】:2014-08-17 18:18:07
【问题描述】:

我像这样更改了数据库中的一列:

class ChangeTestTypeInScores < ActiveRecord::Migration
def self.up
 change_column :scores, :test_type, :boolean
end

def self.down
 change_column :scores, :test_type, :string
end
end

它工作正常,但是当我推送到 heroku 和 Heroku 运行 rake db: migrate 时,我收到以下错误:

PG::DatatypeMismatch: ERROR:  column "test_type" cannot be cast automatically to type     boolean
HINT:  Specify a USING expression to perform the conversion.
: ALTER TABLE "scores" ALTER COLUMN "test_type" TYPE boolean
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

既然我已经更新了本地数据库,我该如何解决这个问题?

【问题讨论】:

  • change_column :scores, :test_type, 'boolean USING CAST(column_name AS boolean)'
  • 所以我修改了我在 rails 中的迁移以匹配你上面的内容,然后重新运行 rake db:migrate?
  • 是的。另一种选择是简单地 rake db:drop 和 rake db:migrate(没有我推荐的行)。我想问题是,在 Heroku 上你已经有了一些 test_type 的值,而且它们的类型不能改变。

标签: ruby-on-rails database heroku


【解决方案1】:

问题是,我认为,您在 Heroku 数据库中已经有一些 test_type 的值,它们是 string,并且不能更改为 boolean

有两种方法可以解决这个问题。

首先:将迁移改为:

change_column :scores, :test_type, 'boolean USING CAST(test_type AS boolean)'

然后运行rake db:migrate

第二个(我更喜欢这个)是删除 Heroku 数据库并再次运行迁移(以您在一开始编写的形式,因为不需要转换任何内容)。

【讨论】:

  • 我尝试只删除数据库并重新启动它,但无论出于何种原因,这都不起作用。然后我尝试了你的第一个建议,它奏效了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-06-19
  • 2017-11-01
  • 1970-01-01
  • 2021-12-03
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
相关资源
最近更新 更多