简体   繁体   English

ruby on rails在表中添加列的最佳方法是什么

[英]ruby on rails what's the best way to add a column to a table

My question is about finding a convenient way to add, remove, rename and change type of columns in a SQLite data structure. 我的问题是找到在SQLite数据结构中添加,删除,重命名和更改列类型的便捷方法。

I have a Ruby on Rails form that has multiple input text fields like the following (simplified): 我有一个Ruby on Rails表单,其中包含多个输入文本字段,如下所示(简化):

<%= form_for(@Product, :html => {:name => "product_name", :id => "product_id"}) do |form| %>
    <div class="field">
        <%= form.flag "Input 1" %>
        <%= form.text_field :Input_1 %>
        <%= form.flag "Input 2" %>
        <%= form.text_field :Input_2 %>
        ..., etc.
    </div>
<% end %>

So far, I followed the procedure described in RoR Migration Guide, so every time I needed to add a new text field, say Input n, I generated a migration for it, like this 到目前为止,我遵循了《 RoR迁移指南》中所述的过程,因此每次需要添加一个新的文本字段(例如Input n)时,都会为其生成一个迁移,就像这样

rails generate migration AddInputToProducts Input_n:string

then I ran the migration 然后我进行了迁移

rake db:migrate

The problem with this procedure is that once the migration is generated and ran, I no more have a trace of the exact name I used for the migration (Input_n in this example) unless I immediately create an entry for it in my RoR form and keep it there for ever. 此过程的问题在于,一旦生成并运行了迁移,除非我立即以RoR形式为其创建一个条目并保留,否则我将不再拥有用于迁移的确切名称(在本示例中为Input_n)的痕迹。它在那里永远。 The results is that my table now has multiple entries I created thinking I would use them but I actually don't need them anymore, I don't remember their exact name and I'd like to either remove, re-use, rename or change their type. 结果是我的表现在有多个条目,我以为可以使用它们而创建了它们,但实际上我不再需要它们了,我不记得它们的确切名称了,我想删除,重复使用,重命名或改变他们的类型。

My question is twofold: 我的问题是双重的:

  1. Is there is a better and more convenient way to add columns to an existing table that would be more practical than running migrations as described in the guide? 是否有更好,更方便的方法向现有表添加列,该方法比运行指南中介绍的迁移更实用?
  2. How can I list, remove, rename or change type of existing columns in my data structure? 如何列出,删除,重命名或更改数据结构中现有列的类型?

If you look in the db folder there is a file named schema.rb which have all the information about your tables including the names of the columns. 如果您在db文件夹中查找,则有一个名为schema.rb的文件,其中包含有关表的所有信息,包括列名。 There is the list. 有清单。

For removing, renaming etc, migrations are perfect, but there is nothing stopping you from using other tools like Mysql Query browser (granted you are using MySQL). 对于删除,重命名等,迁移是完美的,但是没有什么可以阻止您使用其他工具,例如Mysql Query浏览器(允许您使用MySQL)。

如果您不想使用该工具进行迁移,则可以为所使用的数据库(SQLite)使用GUI,也可以在那里进行更改。

Whenever you generate a migration, an associated file is created in db/migrate that you can inspect and adjust as required. 无论何时生成迁移,都会在db/migrate中创建一个关联文件,您可以根据需要检查和调整该文件。 As a matter of convention it's usually bad form to alter a migration that has already been applied. 按照惯例,更改已应用的迁移通常是不好的形式。 If you need to make changes, either unapply the migration if it's only been applied to your machine with rake db:migrate:down VERSION=nnn where nnn is the version serial, or create a second migration that undoes the first. 如果需要进行更改,或者仅在rake db:migrate:down VERSION=nnn其中nnn是版本序列号)应用于计算机的情况下,取消应用rake db:migrate:down VERSION=nnn ,或者创建第二个撤消第一个迁移的迁移。

The more complicated your development and deployment environment is, the more careful you will have to be with managing your migrations. 开发和部署环境越复杂,管理迁移就必须越小心。

If you don't care about your data, you can always rake db:migrate:reset to build from the ground up. 如果您不关心数据,则可以始终使用rake db:migrate:reset进行rake db:migrate:reset构建。 I do this often during the early stages of development when fine-tuning the database structure. 在开发的早期阶段,我经常在微调数据库结构时这样做。 Once I get it working I'll then commit it and make additional migrations to alter it from that point forward. 一旦它开始工作,我将提交它并进行其他迁移以从那时开始对其进行更改。 Remember that reset will effectively drop and recreate your database, so don't ever run this on a production system. 请记住, reset将有效地删除并重新创建数据库,因此永远不要在生产系统上运行它。

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

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