简体   繁体   English

如何使用rails控制台从表中删除列

[英]How can i remove a column from table using rails console

It is easily possible to remove a column using rails migration. 使用rails迁移很容易删除列。

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

I want to know if there is any way to remove a column from table using console. 我想知道是否有任何方法可以使用控制台从表中删除列。

You can run the codes in up method directly in rails console : 您可以直接在rails consoleup方式运行代码:

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

If you already have a migration file such as " db/migrate/20130418125100_remove_foo.rb ", you can do this: 如果您已有迁移文件,例如“ db/migrate/20130418125100_remove_foo.rb ”,则可以执行以下操作:

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

If you just want to do rake db:migrate , try this: 如果你只想做rake db:migrate ,试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM