I am trying to write a migration that will create a table and add a couple of indexes.
This is the migration:
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string "name", :limit => 50
t.string "permalink"
t.integer "position"
t.boolean "visible"
t.integer "subject_id"
add_index("pages","subject_id")
add_index("pages","name")
t.timestamps
end
end
end
When I trying and run this migration I get the following error:
PG::Error: ERROR: relation “pages” does not exist
: CREATE INDEX “index_pages_on_subject_id” ON “pages” (“subject_id”)
Can someone tell me what I’m doing wrong?
Thanks!
You need to call the
add_indexmethod outside yourcreate_tableblock, or callindexwithin the block.The first method:
The second method:
Personally I’d go with the second as this is much neater.