【问题标题】:Cannot change column type during migrating to heroku迁移到 Heroku 期间无法更改列类型
【发布时间】:2013-09-20 00:04:52
【问题描述】:

这是我在尝试运行heroku run rake db:migrate时得到的结果

sender_idstring

==  ChangeTypeToInteger: migrating ============================================
-- change_column(:messages, :sender_id, :integer)
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  column "sender_id" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.
: ALTER TABLE "messages" ALTER COLUMN "sender_id" TYPE integer/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `exec'

【问题讨论】:

  • 这似乎是这个问题的欺骗:stackoverflow.com/questions/12603498/…
  • 我的开发数据库是 sqlite ,我想如果我在 sqlite3 的迁移文件中使用它会出错
  • 出于这样的原因,我强烈建议您不要在不同的环境中使用不同的数据库

标签: ruby-on-rails heroku ruby-on-rails-3.2


【解决方案1】:

我引用手册about ALTER TABLE

如果没有隐式或赋值,则必须提供 USING 子句 从旧类型转换为新类型。

你需要的是:

ALTER TABLE 消息 ALTER COLUMN sender_id TYPE 整数

只要所有条目都可转换为integer,此无论有无数据均可使用
如果您已为该列定义了 DEFAULT,您可能需要删除并为新类型重新创建它。

这里是blog article on how to do this with ActiveRecord

【讨论】:

    猜你喜欢
    • 2011-07-08
    • 2017-07-28
    • 2016-02-29
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多