ruby-on-rails - 为什么会发生不可逆迁移?

标签 ruby-on-rails rails-migrations

错误表明此代码无法回滚:

class AddCountToTag < ActiveRecord::Migration
    def change
        change_table :tags do |t|
            t.integer :count
            t.index :count
        end
    end
end

哪里错了?

最佳答案

可逆迁移尚不支持

change_tableSee this comment at the top of the file (引用如下):

# <tt>ActiveRecord::Migration::CommandRecorder</tt> records commands done during
    # a migration and knows how to reverse those commands. The CommandRecorder
    # knows how to invert the following commands:
    #
    # * add_column
    # * add_index
    # * add_timestamps
    # * create_table
    # * create_join_table
    # * remove_timestamps
    # * rename_column
    # * rename_index
    # * rename_table

如果您需要能够反转,可以使用add_column而不是change_table:

class AddCountToTag < ActiveRecord::Migration
    def change
        add_column :tags, :count, :integer
        ...
    end
end

关于ruby-on-rails - 为什么会发生不可逆迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9841584/

相关文章:

ruby-on-rails - Rails 3.1.0 迁移中remove_index 的正确语法是什么?

ruby - 通过 rake 任务将 CSV 文件上传到 Rails 4 DB

ruby-on-rails - 同时使用 webrat 和 capybara

ruby-on-rails - 使用URL哈希(window.location.hash)进行Rails路由

ruby-on-rails - Rails 4 content_for 和 yield 显示空白页

ruby-on-rails-3 - 使用 Rails 迁移创建表时如何添加 created_at 和 updated_at

ruby-on-rails - Acts_as_taggable_on 索引太长

ruby-on-rails - 路由定义上的 Controller 键丢失

ruby-on-rails - 在我的应用程序上使用 Devise 注销时出现问题

ruby-on-rails - 对 Rails 4/Postgres 的 schema.rb 中的列重新排序是否安全?