0

这是我尝试跑步时得到的heroku run rake db:migrate

sender_id曾经是一个string

==  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'
4

1 回答 1

0

我引用手册关于ALTER TABLE

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

你需要的是:

ALTER TABLE 消息 ALTER COLUMN sender_id TYPE 整数

只要所有条目都可以转换integer.
如果您已DEFAULT为该列定义了 a,则可能必须为新类型删除并重新创建它。

这是有关如何使用 ActiveRecord 执行此操作的博客文章

于 2013-09-20T00:18:00.497 回答