【问题标题】:Rails + Postgres migration - why am I receiving the error "PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist"?Rails + Postgres 迁移 - 为什么我收到错误“PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist”?
【发布时间】:2017-11-01 21:38:01
【问题描述】:

我的一个 Rails 迁移使用 uuid 作为主键。 Postgres 扩展gen_random_uuid() 应该可以解决这个问题,但是我在安装了相关扩展(uuid-ossp)后仍然收到错误。

【问题讨论】:

    标签: ruby-on-rails postgresql uuid rails-migrations


    【解决方案1】:

    问题在于,每次我作为重置和迁移的一部分删除数据库时,uuid-ossp 扩展名都会被数据库炸毁(例如rake db:drop db:create db:migrate)。

    解决方法是创建一个在所有其他迁移之前运行的迁移,以启用相关的扩展。像这样(db/migrate/0_enable_extensions.rb):

    class EnableExtensions < ActiveRecord::Migration[5.1]
      def change
        enable_extension 'uuid-ossp'
        enable_extension 'pgcrypto'
      end
    end
    

    【讨论】:

    • 将迁移的名称更改为 0_XXX 成功了 :)
    【解决方案2】:

    极端情况答案:

    添加启用上述扩展的迁移。

    如果您之前拥有bigint id 并且您正在转换为UUID,那么运行rake db:reset db:migrate 对我来说失败了。请务必如上所述运行rake db:drop db:create db:migrate

    如果您收到错误 Environment data not found in the schema,请运行 bin/rails db:environment:set RAILS_ENV=development

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 2018-11-10
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多