【问题标题】:How can i remove a column from table using rails console如何使用 rails 控制台从表中删除列
【发布时间】:2013-04-18 04:42:45
【问题描述】:

使用 rails 迁移可以轻松移除列。

class SomeClass < ActiveRecord::Migration
  def self.up
    remove_column :table_name, :column_name
  end
end

我想知道是否有任何方法可以使用控制台从表中删除列。

【问题讨论】:

    标签: ruby-on-rails activerecord ruby-on-rails-3.2 rails-migrations


    【解决方案1】:

    您可以直接在rails console中运行up方法中的代码:

    >> ActiveRecord::Migration.remove_column :table_name, :column_name
    

    如果您已经有“db/migrate/20130418125100_remove_foo.rb”之类的迁移文件,您可以这样做:

    >> require "db/migrate/20130418125100_remove_foo.rb"
    >> RemoveFoo.up
    

    如果你只想做rake db:migrate,试试这个:

    >> ActiveRecord::Migrator.migrate "db/migrate"
    

    【讨论】:

    • 如果我没有任何迁移文件,我想执行它。 remove_column :table_name, :column_name 在控制台中不起作用。它说:NoMethodError: undefined method `remove_column' for main:Object
    • ActiveRecord::Migration.remove_column :table_name, :column_name 这很好用!!
    • 你可能刚刚救了我的命。哈哈,好吧,这有点过于戏剧化了,但请注意,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多