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

我正在跟随Ryan Bates的 excellent tutorial使用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

我的迁移看起来像这样:

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中止错误

==  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代码段中使用它时双重引用该表名称.如果正在构建sql,ActiveRecord将为您做引用.如果您希望在许多sql代码段中使用表名,那么我建议您重命名该表,以便您不必关心引用问题.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...