lin*_*ida 36 ruby-on-rails rails-migrations
这是我在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
Run Code Online (Sandbox Code Playgroud)
这是迁移错误:
== 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)
Run Code Online (Sandbox Code Playgroud)
什么是创建索引的正确方法?
Bra*_*dan 72
您仍然可以将索引添加为"更改"迁移的一部分.您只需要在以下呼叫之外执行此操作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
Run Code Online (Sandbox Code Playgroud)
这会正确地创建表,然后在"向上"迁移时创建索引并删除索引,然后在"向下"迁移时删除表.
| 归档时间: |
|
| 查看次数: |
18702 次 |
| 最近记录: |