开发者

schema.rb index different from individual migration index

I have this for my migration:

class CreateCategories < ActiveRecord::Migration
  def up
    create_table :categories do |t|
      t.integer :parent_id
      t.string  :title, :null => false
    end
    execute('CREATE UNIQUE INDEX ix_categories_root_title ON categories (title) WHERE parent_id IS NULL') 
  end
  def down
    drop_table :categories
  end
end

But when I peeked into db/schema.rb I saw this instead:

ActiveRecord::Schema.define(:version => 20110808161830) do
  create_table "categories", :force => true do |t|
    t.integer "parent_id"
    t.string  "title", :null => false
  end
  add_index "categories", ["title"], :name => "ix_categories_root_title", :unique => true
end

Which obviously isn't the same thing and incorrect. Is there anyway to force schema.rb to create the same index? I'm using postresql with Rails 3.开发者_StackOverflow1 pre.


iafonov was correct when he said to enable this config option in your application.rb file:

config.active_record.schema_format = :sql

However, simply enabling this feature does not work as expected. It will not automatically generate a schema.sql file. Instead you can use rake db:structure:dump which will create a structure.sql file. Then you can load it with rake db:structure:load

There's a nice explanation here: schema.sql not creating even after setting schema_format = :sql


I don't know the exact reason of your problem, but you can definitely store your index if you'll store your schema in sql

config.active_record.schema_format = :sql

Btw: which db do you use? Actually this is more like be problem of db driver than problem of rails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜