Ruby on rails schema.rb索引不同于单个迁移索引
我为我的迁移准备了以下内容:Ruby on rails schema.rb索引不同于单个迁移索引,ruby-on-rails,ruby-on-rails-3,activerecord,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Activerecord,Ruby On Rails 3.1,我为我的迁移准备了以下内容: 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
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
这显然不是同一件事,也是不正确的。是否仍有强制schema.rb创建相同索引的方法?我正在将postresql与Rails 3.1 pre一起使用。我不知道问题的确切原因,但如果您将模式存储在sql中,您肯定可以存储索引
config.active_record.schema_format = :sql
顺便问一下:你使用哪种数据库?实际上,这更像是db驱动程序的问题,而不是rails的问题。iafonov说要在application.rb文件中启用此配置选项是正确的:
config.active_record.schema_format = :sql
但是,仅仅启用此功能并不能像预期的那样工作。它不会自动生成schema.sql
文件。相反,您可以使用rakedb:structure:dump
,它将创建一个structure.sql
文件。然后可以使用rakedb:structure:load
这里有一个很好的解释:我使用的是rails 3.1 pre,postgresqlRails只支持数据库引擎的一组基本功能,因此唯一的解决方案是在sql中转储模式。唯一的缺点是失去了可读的模式,但您肯定会拥有所有高级索引。即使将“config.active\u record.schema\u format=:sql”放在application.rb中,我也无法在sql中创建模式。有人能帮忙吗?这里也是一样,它仍然生成一个.rb文件而不是.sql文件。我认为它的rake db:schema:dump可以生成sql版本
config.active_record.schema_format = :sql