【问题标题】:Rails generate MigrationRails 生成迁移
【发布时间】:2013-02-16 05:01:28
【问题描述】:

我目前有一个名为 Products 的迁移,我只是想在此迁移中添加更多字符串,例如描述和产品类型。做这个的最好方式是什么?

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string  :name
      t.decimal :price
      t.text    :description
      t.timestamps
    end
   end
 end

【问题讨论】:

  • 你看过http://guides.rubyonrails.org/migrations.html#using-the-change-method
  • 是的,我已经阅读了手册,但有些事情我并不清楚。这就是我问的原因。我会很感激一步一步的小指导:)
  • 如果您尚未迁移该迁移,您可以直接更改为该迁移文件,例如t.integer :product_type。我在进行开发时的方法是直接更改为该迁移,即使在它迁移之后,当它准备好推送时,我所做的只是重置我的数据库或重新创建我的数据库。这样可以使迁移文件更干净。分享一下,希望能帮到你。

标签: ruby-on-rails-3.1


【解决方案1】:

直接跑吧

rails g migration add_description_to_products description:string
rails g migration add_product_type_to_products product_type:string

然后运行

rake db:migrate

【讨论】:

  • 对于低于 3 的 rails 版本 rails generate migration 命令将是 script/generate migration ...
  • 新列的默认类型是string,因此您实际上不必在命令中明确说明,除非它是不同的类型。
【解决方案2】:

在任何实际应用程序的开发中,您都会进行大量迁移,这些迁移基本上是 DDL(数据定义语言)语句。在现实生活中,您将拥有多个环境(开发、测试、生产等),并且您很有可能在拥有生产版本的同时更改开发数据库。出于这个原因,Rails 的方式是为对数据库的任何更改生成新的迁移,而不是直接更改现有的迁移文件。

所以,请熟悉迁移。

对于具体的问题,你可以这样做:

rails g migration add_attributes_to_products attr1 attr2 attr3

这将生成一个新的迁移文件,用于将 3 个新属性添加到产品表(到产品模型)。属性的默认类型是string。对于其他人,您已将其指定为:

rails g migration add_attributes_to_products attr1:integer attr2:text attr3:enum

【讨论】:

    【解决方案3】:

    如果您的最后一个操作是migration,请使用rollback

    rake db:rollback
    

    然后在迁移文件中添加属性

    class CreateProducts < ActiveRecord::Migration
      def change
        create_table :products do |t|
          t.string  :name
          t.decimal :price
          t.text    :description
          t.string  :product_type  #adding product_type attribute to the products table
          t.timestamps
        end
       end
     end
    

    之后使用

    迁移
    rake db:migrate
    

    如果迁移不是您的最后一个操作,请按照上述答案生成一个新的迁移文件

    rails g migration add_attributes_to_products product_type:string
    

    以上代码仅生成迁移文件,但您想使用rake db:migrate 来迁移文件。

    如果您想对该迁移文件进行更多更改,例如添加更多属性,请在迁移之前执行,否则如果您的最后一个操作是迁移,则您必须使用我在开头提到的方法,否则您需要生成另一个迁移文件。 检查此链接以了解有关迁移的更多信息 http://guides.rubyonrails.org/v3.2.8/migrations.html

    【讨论】:

    • 希望对某人有所帮助
    【解决方案4】:

    假设您使用上面的迁移创建了表,然后添加 product_type(您已经有描述),您会这样做:

    # db/migrate/20130201121110_add_product_type_to_product.rb
    
    class AddProductTypeToProduct < ActiveRecord::Migration
      def change
        add_column :products, :product_type, :string
        Product.all.each do |product|
          product.update_attributes!(:product_type => 'unknown')
        end
      end
    end
    

    【讨论】:

      【解决方案5】:

      rails 生成迁移 add_description_to_products

      AddDescriptionToProducts < ActiveRecords:: Migration[v]
        def change
          add_column :products :description :string
          add_column :name_of_table :name_of_column :data_type
        end
      

      运行 rake db:migrate,您的 schema.rb 将自动更新

      【讨论】:

        猜你喜欢
        • 2016-08-02
        • 2011-07-27
        • 1970-01-01
        • 2013-12-14
        • 2015-12-07
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        相关资源
        最近更新 更多