schema.rb index different from individual migration index

拟墨画扇 提交于 2019-12-12 19:09:43

问题


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.1 pre.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/7012812/schema-rb-index-different-from-individual-migration-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!