ruby-on-rails - 无法运行 rake db :migrate a tsvector GIN index for Postgresql in Rails using Railscast sample

标签 ruby-on-rails postgresql postgresql-9.1 pg-search

我正在关注 Ryan Bates 的 excellent tutorial关于在 Rails 中使用内置的 PostgresQL 全文搜索。我目前正在使用未编入索引的 pg_search gem 没问题,但我需要提高性能。我正在使用指定了“英语”字典的 tsvector。

我使用的是 PostgreSQL 版本 9.1.4

根据 Ryan 的指示,我运行了一个新的迁移,此代码指定了我想要创建的两个新索引。首先是架构:

create_table "references", :force => true do |t|
  t.string   "title"
  t.string   "type"
  t.datetime "created_at",         :null => false
  t.datetime "updated_at",         :null => false
  t.string   "public_url"
  t.string   "content_type"
  t.integer  "file_size"
  t.text     "overview"
  t.text     "body"
  t.text     "full_text"
  t.integer  "folder_id"
end

我的迁移看起来像这样:

def up
  execute "create index references_title on references using gin(to_tsvector('english', title))"
  execute "create index references_full_text on references using gin(to_tsvector('english', full_text))"
end

def down
  execute "drop index references_title"
  execute "drop index references_full_text"
end

我还取消了 application.rb 中的 :sql 选项的注释

config.active_record.schema_format = :sql

我继续收到相同的 rake aborted 错误:

==  AddSearchIndexesToReferences: migrating ===================================
-- execute("CREATE INDEX references_title on references using gin(to_tsvector('english',    title))")
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  syntax error at or near "references"
LINE 1: CREATE INDEX references_title on references using gin(to_tsv...
                                     ^
: CREATE INDEX references_title on references using gin(to_tsvector('english', title))

最佳答案

REFERENCES 是与 foreign keys 一起使用的关键字所以你不能将它用作表名,除非你用双引号将它引起来:

def up
  execute %q{create index references_title on "references" using gin(to_tsvector('english', title))}
  execute %q{create index references_full_text on "references" using gin(to_tsvector('english', full_text))}
end

您还必须在 SQL 片段中使用该表名的任何地方用双引号引起来。不过,如果 ActiveRecord 正在构建 SQL,它将为您进行引用。如果您希望在很多 SQL 片段中使用表名,那么我建议您重命名该表,这样您就不必关心引用问题。

关于ruby-on-rails - 无法运行 rake db :migrate a tsvector GIN index for Postgresql in Rails using Railscast sample,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13189105/

相关文章:

ruby-on-rails - 按不同顺序条件合并两个数组

ruby-on-rails - 制作 Rails 应用程序多语言的最佳实践

postgresql - 在不创建临时表的情况下加入数组 unnest 的输出

ruby-on-rails - 为什么在 ApplicationController 上定义为私有(private)的方法可以在派生类的方法内部调用,但不能在派生类本身内部调用?

ruby-on-rails - SongsController 中的 NameError#index 未初始化常量 Song::FriendlyId

sql - 引用前几行(滞后 4)

sql - 如何将*大*数据 block 导入 PostgreSQL?

postgresql - 使用非唯一键的 Postgres 分页?

hibernate - Spring Data JPA、Hibernate Vendor、 Multi-Tenancy 和 Postgresql

sql - PostgreSQL with-delete "relation does not exists"