here is my migration in 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
and here is the migrate error:
== 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)
what is the right way to create a index?
You can still add an index as a part of a "change" migration. You just have to do it outside of the call to 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
This correctly creates the table and then the index on an "up" migration and drops the index and then the table on a "down" migration.
so I change it to the old way, and it works. and I think there is a new way doing this by using change method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With