无法运行rake db:使用Railscast示例在Rails中为Postgresql迁移tsvector GIN索引

Pau*_*aul 9 postgresql ruby-on-rails pg-search postgresql-9.1

我正在关注Ryan Bates 关于在Rails中使用内置的PostgresQL全文搜索的优秀教程.我目前正在使用pg_search gem un-indexed没问题,但我需要提高性能.我正在使用指定了"英语"字典的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
Run Code Online (Sandbox Code Playgroud)

我的迁移看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)

我也继续在application.rb中取消注释:sql选项

config.active_record.schema_format = :sql
Run Code Online (Sandbox Code Playgroud)

我继续得到相同的rake中止错误:

==  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))
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 7

REFERENCES是一个与外键一起使用的关键字,因此除非你引用它,否则不能将它用作表名:

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
Run Code Online (Sandbox Code Playgroud)

您还必须在SQL代码段中的任何位置使用该引号来双引号.如果正在构建SQL,ActiveRecord将为您做引用.如果您希望在许多SQL代码段中使用表名,那么我建议您重命名该表,以便您不必关心引用问题.