【发布时间】:2026-02-16 22:00:01
【问题描述】:
所以我做了这样的迁移
class AddDatetimeAttrToUsers < ActiveRecord::Migration
def change
change_column :users, :oauth_expires_at, :datetime
end
end
在我的本地环境中它工作得很好,但是当我尝试时
heroku run rake db:migrate 我得到一个错误
ERROR: column "oauth_expires_at" cannot be cast automatically to type timestamp without time zone
HINT: Specify a USING expression to perform the conversion.
当我搜索它时,我创建了一个像这样的新迁移,作为使用 change 更改属性的最佳实践。
class PutDatetimeFieldToUsersExpireAtColumn < ActiveRecord::Migration
def change
remove_column :users, :oauth_expires_at
add_column :users, :oauth_expires_at, :datetime
end
end
所以我尝试使用 rake db:rollback 删除最后一次迁移并添加这个通知我最后一次迁移是不可逆的。
我的问题是,有没有办法真正回滚不可逆转的迁移,还是我应该只使用上面的新迁移进行迁移?
【问题讨论】:
-
不可逆迁移是不可逆的,因为您破坏了数据。您可能对this answer 感兴趣,它可以让您知道如何在迁移中使用
USING(如提示所建议的那样)。 (编辑:将链接更改为更好的链接) -
如何添加使用子句?
标签: ruby-on-rails ruby heroku