2

我已經爲我的遷移:schema.rb指數從個體遷移指數不同

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 

但是當我偷看進入DB/schema.rb我看到這個:

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 

這顯然不是一回事,也不正確。無論如何強制schema.rb創建相同的索引?我在Rails 3.1之前使用postresql。

回答

1

我不知道你的問題的確切原因,但你絕對可以存儲索引,如果你存儲在SQL

config.active_record.schema_format = :sql 

順便說一下您的模式:你使用哪個數據庫?其實這比起rails的問題更像是db driver的問題。

config.active_record.schema_format = :sql 

然而,僅僅啓用此功能並不會達到預期效果:當他說讓你的application.rb中的文件該配置選項

+0

我是在軌道上3.1預PostgreSQL的 – sthapit

+0

Rails的只支持一組基本的數據庫引擎的功能,所以唯一的解決辦法是轉儲在SQL中的架構。唯一的缺點是丟失可讀的模式,但你一定會擁有所有的高級索引。 – iafonov

+0

即使在application.rb中添加了「config.active_record.schema_format =:sql」後,我仍無法在sql中創建模式。有人可以幫忙嗎? http://stackoverflow.com/questions/7034977/how-to-create-schema-in-sql – sthapit