【问题标题】:rails 3.2 migration cannot add index to create_table in change methodrails 3.2 迁移无法在更改方法中向 create_table 添加索引
【发布时间】:2012-03-15 02:02:30
【问题描述】:

这是我在 rails 3.2.2 中的迁移:

class CreateStatistics < ActiveRecord::Migration
  def change
    create_table :statistics do |t|
      t.string :name
      t.integer :item_id
      t.integer :value
      t.text :desc

      t.timestamps
      t.index [:name, :item_id]
    end

  end
end

这是迁移错误:

==  CreateStatistics: migrating ===============================================
-- create_table(:statistics)
ActiveRecord::ConnectionAdapters::TableDefinition
rake aborted!
An error has occurred, all later migrations canceled:

undefined method `index' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0xbd16888>

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

创建索引的正确方法是什么?

【问题讨论】:

    标签: ruby-on-rails rails-migrations


    【解决方案1】:

    您仍然可以添加索引作为“更改”迁移的一部分。您只需要在调用 create_table 之外执行此操作:

    class CreateStatistics < ActiveRecord::Migration
      def change
        create_table :statistics do |t|
          t.string :name
          t.integer :item_id
          t.integer :value
          t.text :desc
    
          t.timestamps
        end
    
        add_index :statistics, [:name, :item_id]
      end
    end
    

    这会在“向上”迁移时正确创建表和索引,并在“向下”迁移时删除索引和表。

    【讨论】:

    • 快速说明:@Brandan 的答案比 injeckt 的 Rails 3 风格迁移“更正确”,允许使用 change 方法而不是旧风格的 updown 方法。两者都很好,我只花了一分钟就意识到了区别。
    【解决方案2】:

    所以我将其更改为旧方式,并且可以正常工作。 我认为有一种新的方法可以通过使用 change 方法来做到这一点。

    class CreateStatistics < ActiveRecord::Migration
      def up
        create_table :statistics do |t|
          t.string :name
          t.integer :item_id
          t.integer :value
          t.text :desc
    
          t.timestamps
        end
        add_index :statistics, [:name, :item_id]
      end
    
      def down
        drop_table :statistics
      end
    end
    

    【讨论】:

    • 这正是我在回答中所说的
    【解决方案3】:

    如果您有多个索引并且不想在单独的 add_index 调用中多次重复表名,则可以使用 create_table 后面的 change_table 块。

    create_table :user_states do |t|
      t.references :user, :null => false
      t.integer :rank
      t.integer :status_code
    end
    
    change_table :user_states do |t|
      t.index [:rank, :status_code]
    end
    

    【讨论】:

      【解决方案4】:
          class CreateTempPfp < ActiveRecord::Migration
            def change
              create_table :temp_ptps do |t|
                t.string :owner
                t.integer :source_id
                t.string :source_type
                t.integer :year
                t.string :pcb_type
                t.float :january
                t.float :february
                t.float :march
                t.float :april
                t.float :may
                t.float :june
                t.float :july
                t.float :august
                t.float :september
                t.float :october
                t.float :november
                t.float :december
                t.float :dollar_per_sqft
                t.float :dollar_per_unit
                t.integer :rp_acc_code
                t.integer :rp_property_id
                t.integer :real_estate_property_id
                t.timestamps
             end
             add_index :temp_ptps, [:source_id, :source_type]
           end
         end
      

      【讨论】:

        【解决方案5】:

        看起来create_table 产生了一个ActiveRecord::ConnectionAdapters::TableDefinition 类。此类不包含方法index。相反,change_table 似乎生成了一个 ActiveRecord::ConnectionAdapters::Table 类,其中包含此 index 方法。

        如果您想在 create_table 迁移期间添加索引,请尝试以下操作:

        class CreateStatistics < ActiveRecord::Migration
          def self.up
            create_table :statistics do |t|
              t.string :name
              t.integer :item_id
              t.integer :value
              t.text :desc
        
              t.timestamps
            end
        
            add_index :statistics, :name
            add_index :statistics, :item_id
          end
        
          def self.down
            drop_table :statistics
          end
        end
        

        【讨论】:

        • 在您的 create_table 通话之外使用 add_index :table_name, ...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 2018-06-25
        相关资源
        最近更新 更多