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

时间:2021-10-18 22:56:28

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.

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

1 个解决方案

#1


96  

You can run the codes in up method directly in rails console:

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

>> 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”。你可以这样做:

>> 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"

#1


96  

You can run the codes in up method directly in rails console:

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

>> 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”。你可以这样做:

>> 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"