繁体   English   中英

ActiveRecord::StatementInvalid: PG::UndefinedFunction: 错误: 运算符不存在: 文本 % 未知

[英]ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: text % unknown

我正在尝试使用pg_search的函数 : trigram来查找与我存储在数据库中的名称相似的名称。 不幸的是,由于我还在学习 SQL,这个错误目前对我来说有点难以处理。

我的卡型号:

class Card < ApplicationRecord
  include PgSearch
  pg_search_scope :spelled_like,
                  :against => :name,
                  :using => :trigram
end

我在 rails C 中尝试做的事情:

2.4.1 :005 > c = "Timeslream Navigator"
 => "Timeslream Navigator" 
2.4.1 :006 > Card.spelled_like(c)
  Card Load (1.9ms)  SELECT  "cards".* FROM "cards" INNER JOIN (SELECT "cards"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("cards"."name"::text, ''))), (to_tsquery('simple', ''' ' || 'Timeslream' || ' ''') && to_tsquery('simple', ''' ' || 'Navigator' || ' ''')), 0)) AS rank FROM "cards" WHERE (((coalesce("cards"."name"::text, '')) % 'Timeslream Navigator'))) AS pg_search_6ac9a32c0ca8e84e5bf0c3 ON "cards"."id" = pg_search_6ac9a32c0ca8e84e5bf0c3.pg_search_id ORDER BY pg_search_6ac9a32c0ca8e84e5bf0c3.rank DESC, "cards"."id" ASC LIMIT $1  [["LIMIT", 11]]
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: text % unknown
LINE 1: ...rds" WHERE (((coalesce("cards"."name"::text, '')) % 'Timeslr...

澄清 - 答案应该是 Times TR eam Navigator(它确实存在于我的数据库中)

Postgres 给了我一个提示:

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT  "cards".* FROM "cards" INNER JOIN (SELECT "cards"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("cards"."name"::text, ''))), (to_tsquery('simple', ''' ' || 'Timeslream' || ' ''') && to_tsquery('simple', ''' ' || 'Navigator' || ' ''')), 0)) AS rank FROM "cards" WHERE (((coalesce("cards"."name"::text, '')) % 'Timeslream Navigator'))) AS pg_search_6ac9a32c0ca8e84e5bf0c3 ON "cards"."id" = pg_search_6ac9a32c0ca8e84e5bf0c3.pg_search_id ORDER BY pg_search_6ac9a32c0ca8e84e5bf0c3.rank DESC, "cards"."id" ASC LIMIT $1

但是当我尝试调用它时,我收到另一个错误,此时并没有告诉我太多:

SyntaxError: (irb):7: syntax error, unexpected tCONSTANT, expecting end-of-input
  "cards".* FROM "cards" INNER JOIN (SELECT "cards"."id" AS 
                              ^

卡片架构:

ActiveRecord::Schema.define(version: 2018_05_31_102526) do

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

  create_table "cards", force: :cascade do |t|
    t.string "name"
    t.string "set"
    t.float "price"
    t.string "image"
    t.string "type_line"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

有任何想法吗?

回答我自己的问题:

  • pg_search 很棒!
  • 为了使用三元组功能,您显然必须安装它。
  • 我的问题是,我直接在 psql 中安装了它,但没有在我的 Rails 应用程序中运行正确的迁移。

这是让我解决问题并开始享受 trigram 惊人功能的迁移片段:

class InstallPgTrgmContribPackage < ActiveRecord::Migration[5.2]
  def up
    execute "CREATE EXTENSION pg_trgm;"
  end

  def down
    execute "DROP EXTENSION pg_trgm;"
  end
end

我的问题有点不同。 即使我的扩展已经安装,我也会在 AWS 环境中遇到这个错误,而不是较低的环境。 所以我放弃了扩展并重新创建了它。
那奏效了。 这是我的错误:

PG::UndefinedFunction:错误:运算符不存在:未知 <% 文本由 pg_search gem 使用三元组引起。

或者你可以禁用使用三元组,但我想保留它们,所以我一直在研究

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM