从表中使用Rails控制台我怎样才能删除列 [英] How can i remove a column from table using rails console

查看:111
本文介绍了从表中使用Rails控制台我怎样才能删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很容易可以使用Rails迁移中删除列。

It is easily possible to remove a column using rails migration.

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.

推荐答案

您可以直接在运行codeS的最高方法轨控制台

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

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

如果您已经有一个迁移文件,如 DB /迁移/ 20130418125100_remove_foo.rb ,你可以这样做:

If you already have a migration file such as "db/migrate/20130418125100_remove_foo.rb", you can do this:

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

如果你只想做耙分贝:迁移,试试这个:

If you just want to do rake db:migrate, try this:

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

这篇关于从表中使用Rails控制台我怎样才能删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆