ActiveRecord:添加了“uuid-ossp”扩展,但没有可用的uuid功能

问题描述 投票:0回答:1

使用rails-5.0.7.1(根据bundle show rails

我编写了一个迁移,它添加了"uuid-ossp"扩展,并且执行了SQL,当我在\dx控制台中键入psql时,扩展显示出来。但是,当我键入uuid_generate_v4时,此扩展提供的函数(例如\df)不会显示,因此任何尝试使用应添加的函数都会失败。

当我从ActiveRecord迁移中获取SQL并将其直接复制并粘贴到psql控制台时,一切都按预期工作 - 添加了扩展,并且功能可用。

Here is my migration code:

class EnableUuidOssp < ActiveRecord::Migration[5.0]
  def up
    enable_extension "uuid-ossp"
  end

  def down
    disable_extension "uuid-ossp"
  end
end

这是输出:

$ bundle exec rake db:migrate
== 20190308113821 EnableUuidOssp: migrating ==============================
-- enable_extension("uuid-ossp")
   -> 0.0075s
== 20190308113821 EnableUuidOssp: migrated (0.0076s) =====================

^这一切似乎都成功运行,但没有启用任何功能。这意味着包含... SET uuid = uuid_generate_v4() ...等语句的未来SQL将失败并显示此错误HINT: No function matches the given name and argument types. You might need to add explicit type casts.

What does work

直接进入psql并输入:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

^这将安装扩展并使功能可用。

And yet, what doesn't work

好的,所以如果我采用上面的SQL并以这种方式重写我的迁移:

  ...

  def up
    execute <<~SQL
      CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    SQL
  end

  ...

^此迁移将无错误地运行,但它仍然不会使这些功能可用。

因此,在psql中运行的相同复制+粘贴SQL无法通过ActiveRecord execute方法工作,这真的让我很困惑。我不确定我错过了什么因为这会导致失败。

postgresql activerecord ruby-on-rails-5 postgresql-extensions
1个回答
1
投票

我假设安装扩展的架构不在你的search_path上。

您可以看到该架构

\dx "uuid-ossp"

尝试使用模式限定函数,例如在public.uuid_generate_v4()中。

© www.soinside.com 2019 - 2024. All rights reserved.