简体   繁体   English

Rails迁移以删除字段

[英]Rails migration to remove field

Hello I am about to attempt to run a rails migration on a database of skills that has a :title and :description. 您好,我将尝试在具有:title和:description的技能数据库上运行Rails迁移。 I need to remove the description field and I assume it will look something like this: 我需要删除描述字段,并且我假设它看起来像这样:

rails migration remove_column :skills, :description

I am running it by you pros before I attempt it and end up breaking something on accident. 在尝试之前,我会一直由您的专业人员运行,并最终导致意外损坏。 Please let me know if I have the right idea about removing the description field from the database. 如果我对从数据库中删除说明字段有正确的想法,请告诉我。 Thanks a lot! 非常感谢!

If skills is the name of your table and description is the name of the column you want to remove, you can type rails g migration RemoveDescriptionFromSkills in your terminal. 如果skills是表的名称, description是要删除的列的名称,则可以在终端中键入rails g migration RemoveDescriptionFromSkills This will generate a migration file with the name [timestamp]_remove_description_from_skills.rb , located in db/migrate . 这将生成一个名为[timestamp]_remove_description_from_skills.rb的迁移文件,位于db/migrate Edit this file so that it contains the following: 编辑此文件,使其包含以下内容:

class RemoveDescriptionFromSkills < ActiveRecord::Migration
  def change
    remove_column :skills, :description
  end
end

Then type rake db:migrate in your terminal, and the column will be removed. 然后在终端中输入rake db:migrate ,该列将被删除。

For more information, check out this helpful Stack Overflow post: https://stackoverflow.com/a/1992045/3723769 . 有关更多信息,请查看此有用的Stack Overflow帖子: https : //stackoverflow.com/a/1992045/3723769

Note: this answer is intended to describe how to perform the migration. 注意:此答案旨在描述如何执行迁移。 As a safety measure, you should do what Michael Durrant advises before migrating: https://stackoverflow.com/a/25006727/3723769 . 为了安全起见,在迁移之前,您应该按照Michael Durrant的建议进行操作: https : //stackoverflow.com/a/25006727/3723769

Here's some of the things that some to mind:- 以下是一些需要注意的事情:

  • check the existing values to see if there's any data you want 检查现有值以查看是否需要任何数据
  • see if any indexes exist that should also be dropped. 查看是否存在也应删除的索引。
  • search the application for that field name 在应用程序中搜索该字段名称
  • see if there's an existing rails migration that you can use to DOWN the change 查看是否存在现有的Rails迁移,您可以使用该迁移来进行更改

Finally, I would consider creating a change migration as normal, ie one that actaully adds the field and then I would run it using the down syntax to remove the field. 最后,我将考虑按正常方式创建一个change迁移,即实际添加一个字段,然后使用down语法运行它以删除该字段。

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

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