【问题标题】:unable to make postgres unaccent extension working on rails无法使 postgres unaccent 扩展在 Rails 上工作
【发布时间】:2018-03-09 14:03:20
【问题描述】:

在一个正常工作的应用程序中,我正在尝试实现非重音查询。

pg-gem version 0.21
postgresql version 9.5/9.6
rails version 4

我首先在这里创建/启用扩展是迁移:

def up
  execute 'CREATE EXTENSION IF NOT EXISTS unaccent'
end

def down
  execute 'DROP EXTENSION IF EXISTS unaccent'
end

迁移运行良好。 psql \dx 表示扩展已开启。 对psql 的一个简单的unccent 查询表明它正在按预期工作。

当我尝试这样查询时:

people = people.where('unaccent(people.first_name) ILIKE unaccent(?)', "%#{params[:first_name]}%") if params[:first_name].present?

我得到这个错误:

PG::UndefinedFunction: ERROR:  function unaccent(character varying) does not exist

起初我以为连接到了错误的数据库,因为我玩了很多版本,但在确定不是这种情况后,错误仍然存​​在。

为了完全确定,我还在测试服务器上对其进行了测试,但结果相同。

由于在 db 级别上一切似乎都很好,我怀疑 Rails 方面有一些东西,但我不知道如何进一步调查。

欢迎任何帮助/建议!

【问题讨论】:

  • 也许扩展是在不在您的 Rails 应用程序的 search_path 中的架构中创建的?
  • 感谢您的评论,我实际上正在阅读此内容。当我们使用公寓 gem 为租户制作模式时,我认为这可能是解释。让我检查一下,然后回复你。
  • 我天真地认为扩展将适用于所有模式!

标签: ruby-on-rails postgresql ruby-on-rails-4


【解决方案1】:

运行对我有用的新迁移

class AddUnaccent < ActiveRecord::Migration[6.0]
  def change
    enable_extension "unaccent"
  end
end

然后在查询中

Store.where('unaccent(name) ILIKE unaccent(?)', "%#{term}%")

【讨论】:

  • 您没有在查询中使用 UNACCENT。试试people = people.where('unaccent(people.first_name) ILIKE unaccent(?)', "%#{params[:first_name]}%")
  • Store.where('unaccent(name) ILIKE unaccent(?)', "%#{term}%") 在您的上下文中
【解决方案2】:

扩展必须是在不在 rails 应用程序的 search_path 上的架构中创建的。

最好的解决方案是拥有一个 common 架构(或者可能使用现有的 public 架构),其中包含所有架构共有的对象。然后在该架构中安装扩展。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多