Ruby-on-rails Rails 5:如何从数据库中删除一列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38820188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 5: How to remove a column from a database?
提问by davideghz
What is the command for removing an existing column from a table using migration?
使用迁移从表中删除现有列的命令是什么?
The column I want to remove is: country:string
我要删除的列是: country:string
From the table: sample_apps
从表中: sample_apps
回答by davideghz
To remove a column with migration:
要通过迁移删除列:
rails g migration Remove..From.. col1:type col2:type col3:type
In your case:
在你的情况下:
rails g migration RemoveCountryFromSampleApps country:string
This will generate the following migration in Rails 5.0:
这将在 Rails 5.0 中生成以下迁移:
class RemoveCountryFromSampleApps < ActiveRecord::Migration[5.0]
def change
remove_column :sample_apps, :country, :string
end
end
回答by Hasmukh Rathod
Create migration file:
创建迁移文件:
$ rails generate migration RemoveCountryFromSampleApps country:string
In generated migration file:
在生成的迁移文件中:
class RemoveCountryFromSampleApps < ActiveRecord::Migration
def change
remove_column :sample_apps, :country, :string
end
end
Then run:
然后运行:
rake db:migrate
回答by Ayush Chaurasia
To remove a column(country here) from table(sample_case)
从表(sample_case)中删除一列(此处为国家)
rails generate migration RemoveCountryfromSampleCase country:string
Above command should generate a file YYYYMMDDHHMMSS_remove_countryfrom_sample_case.rb. under db/migratefolder
上面的命令应该生成一个文件YYYYMMDDHHMMSS_remove_countryfrom_sample_case.rb。在db/migrate文件夹下
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
remove_column :sample_case, :country, :string
end
end
In my case (I was doing it for more than two columns) only this appears
在我的情况下(我做了两列以上)只有这个出现
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
end
end
remove_columnline was not there so I added it manually and then fired the command
remove_column行不在那里,所以我手动添加了它,然后触发了命令
rails db:migrate
and it worked for me.
它对我有用。
References https://stackoverflow.com/a/2963582& Ruby guide on Active Record migration https://edgeguides.rubyonrails.org/active_record_migrations.html
参考https://stackoverflow.com/a/2963582和关于 Active Record 迁移的 Ruby 指南 https://edgeguides.rubyonrails.org/active_record_migrations.html
回答by Greg
Do you need to specify the type?
需要指定类型吗?
Why not just remove_column :sample_apps, :countryor remove_column :comments, :post_idfor the samples here? Seems to work and removes a chance for error (text or string).
为什么不只是remove_column :sample_apps, :country或remove_column :comments, :post_id为了这里的样品?似乎有效并消除了出错的机会(文本或字符串)。
回答by rld
If want to remove the index too, you do with migration too:
如果也想删除索引,您也可以使用迁移:
rails g migration remove_post_id_from_comments post_id:integer:index
migration file:
迁移文件:
class RemovePostIdFromComments < ActiveRecord::Migration
def change
remove_index :comments, :post_id
remove_column :comments, :post_id, :integer
end
end
then run: rake db:migrate
然后运行: rake db:migrate

