Rails 5:如何从数据库中删除列?

30 ruby-on-rails

使用迁移从表中删除现有列的命令是什么?

我要删除的列是: country:string

从表中: sample_apps

dav*_*ghz 66

要通过迁移删除列:

rails g migration Remove..From.. col1:type col2:type col3:type
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

rails g migration RemoveCountryFromSampleApps country:string
Run Code Online (Sandbox Code Playgroud)

这将在Rails 5.0中生成以下迁移:

class RemoveCountryFromSampleApps < ActiveRecord::Migration[5.0]
  def change
    remove_column :sample_apps, :country, :string
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 在此之后我需要运行`rails db:migrate`吗? (4认同)