postgresql Rails 4 pgsql add_index 类型为 GIN 或 GiST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18725081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:05:56  来源:igfitidea点击:

Rails 4 pgsql add_index with type GIN or GiST

ruby-on-railspostgresqlruby-on-rails-4

提问by Bill

In Rails it is possible to do:

在 Rails 中,可以执行以下操作:

add_index :table, :column_name, :using => 'btree'

add_index :table, :column_name, :using => 'btree'

Is it possible in Rails 4 with PGSQL to add a GINor GiSTindex like:

是否可以在带有 PGSQL 的 Rails 4 中添加一个GINGiST索引,如:

add_index :students, :name, :using => 'gin'

add_index :students, :name, :using => 'gin'

Or do I have use manual execute statements? The notion behind this is I would like to keep schema.rb instead of using structure.sql

或者我有使用手动执行语句吗?这背后的概念是我想保留 schema.rb 而不是使用 structure.sql

回答by Pronix

In Rails 4, you can now do something like this in a migration:

在 Rails 4 中,您现在可以在迁移中执行以下操作:

add_index :products, :data, using: :gin

回答by Josh

Wow! I had a few hairs turn gray on this one. I am using rails 4.2 and trying to run this migration and it was giving me the same error as people above.

哇!我有几根头发变白了。我正在使用 rails 4.2 并尝试运行此迁移,它给了我与上面的人相同的错误。

PG::UndefinedObject: ERROR: data type character varying has no default

I found out that you can in fact still continue to use schema.rb and do not have to use the config/application.rb

我发现您实际上仍然可以继续使用 schema.rb 而不必使用 config/application.rb

config.active_record.schema_format = :sql

The one major thing I was missing was installing postgres contrib modules. I made the assumption that features like gin and gist were already turned on. I never noticed but modules are shown in your schema.rb file. They appear at the top like this

我缺少的一件主要事情是安装 postgres contrib 模块。我假设 gin 和 gist 等功能已经打开。我从未注意到,但模块显示在您的 schema.rb 文件中。它们像这样出现在顶部

ActiveRecord::Schema.define(version: 20151203234708) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  enable_extension "pg_trgm"
  enable_extension "fuzzystrmatch"
  enable_extension "btree_gin"
  enable_extension "btree_gist"

if you do not see btree_gin enabled, you can not use the code

如果你没有看到 btree_gin 启用,你不能使用代码

add_index :products, :data, using: :gin

you can install any module by running a migration like so. The changes will be reflected in your schema.rb

您可以通过像这样运行迁移来安装任何模块。更改将反映在您的 schema.rb 中

class InstallSomeContribPackages < ActiveRecord::Migration
  def up
    execute "CREATE EXTENSION IF NOT EXISTS btree_gin;"
    execute "CREATE EXTENSION IF NOT EXISTS btree_gist;"
  end

  def down
    execute "DROP EXTENSION IF EXISTS btree_gin;"
    execute "DROP EXTENSION IF EXISTS btree_gist;"
  end
end

here is a list of postgres modules

这是postgres 模块的列表