为什么add_index使用“杜松子酒'创建'B树人指数呢? [英] Why does add_index using 'gin' create a 'btree' index instead?
问题描述
我在的PostgreSQL 9.3.4
和的Rails 4.0.4
。
我添加了一个标签一栏,以及相应的杜松子酒
指数(或至少我问一个)。
I add a "tags" column, and corresponding gin
index (or, at least I ask for one).
class AddTagsToPhotos < ActiveRecord::Migration
def change
add_column :photos, :tags, :text, array: true, null: false, default: []
add_index :photos, :tags, using: 'gin'
end
end
验证通过的psql
结果:
psql=# \d photos
...
tags | text[] | not null default '{}'::text[]
Indexes:
"index_photos_on_tags" btree (tags)
注意,标签指数型 B树
- 而我问杜松子酒
现在手动创建一个索引表明杜松子酒
可用:
Now manually create an index to show that gin
is available:
psql=# create index index_photos_on_tags2 on photos using gin(tags) ;
psql=# \d photos
Indexes:
"index_photos_on_tags" btree (tags)
"index_photos_on_tags2" gin (tags)
事实上,杜松子酒
是可用的。
就目前我使用这个解决方法与原始SQL,但想知道为什么典型的做法上述失败:
For the time being I am using this workaround with raw SQL, but would like to know why the typical approach above is failing:
class AddTagsToPhotos < ActiveRecord::Migration
def up
add_column :photos, :tags, :text, array: true, null: false, default: []
ActiveRecord::Base.connection.execute('create index index_photos_on_tags on photos using gin(tags) ;')
end
def down
ActiveRecord::Base.connection.execute('drop index index_photos_on_tags')
remove_column :photos, :tags
end
end
需要注意的是另一个障碍!
原来, DB / schema.rb
不会杜松子酒
设置为索引类型:
It turns out that db/schema.rb
will not have gin
set as the index type:
add_index "photos", ["tags"], :name => "index_photos_on_tags"
潜在临时解决方法:
add_index "photos", ["tags"], :name => "index_photos_on_tags", using: :gin
警告!
到这个bug是固定的,必须查看修改 DB / schema.rb
当你运行一个迁移,未来所有的迁移将剥离使用::杜松子酒
从 add_index
行
Until this bug is fixed, you must review changes to db/schema.rb
whenever you run a migration, as all future migrations will strip using: :gin
from the add_index
line.
推荐答案
由于paxer提到的,你需要将你的模式格式设置为:通过添加(或更改SQL
)这条线在你的的config / application.rb中
:
As paxer mentioned, you will need to set your schema format to :sql
by adding (or changing) this line in your config/application.rb
:
config.active_record.schema_format = :sql
究其原因,在解释 Rails移植指南,是一个GIN索引是特定给Postgres。当您使用数据库特定的项目, schema.rb
将无法重新创建。
The reason, as explained in the Rails Migrations guide, is that a GIN index is specific to Postgres. When you are using database-specific items, schema.rb
will not be able to recreate them.
下面是Rails的指导报价:
Here is a quote from the Rails guide:
然而,有一个权衡:DB / schema.rb不能EX preSS数据库的具体项目如触发器或存储过程。而在迁移可以执行自定义的SQL语句,架构自卸车无法重建从数据库中那些语句。如果你正在使用这样的功能,那么你应该设置模式格式:SQL
There is however a trade-off: db/schema.rb cannot express database specific items such as triggers, or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this, then you should set the schema format to :sql.
一旦你使用:SQL
格式,您的模式现在被保存到 structure.sql
而不是 schema.rb
。这两个文件可以生活并排,但只有 structure.sql
将被更新,并当您的格式设置为应用程序中使用:SQL
。
Once you use the :sql
format, your schema will now be saved to structure.sql
instead of schema.rb
. Both files can live side by side, but only structure.sql
will be updated and used by the app when your format is set to :sql
.
这篇关于为什么add_index使用“杜松子酒'创建'B树人指数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!