Rails + Postgres迁移-为什么我会收到错误“ PG :: UndefinedFunction:ERROR:函数gen_random_uuid()不存在”? [英] Rails + Postgres migration - why am I receiving the error "PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist"?

查看:468
本文介绍了Rails + Postgres迁移-为什么我会收到错误“ PG :: UndefinedFunction:ERROR:函数gen_random_uuid()不存在”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个Rails迁移使用uuid作为主键。 Postgres扩展 gen_random_uuid()应该可以解决此问题,但在安装相关扩展后( uuid-ossp )。

解决方案

问题是 uuid-ossp 每次我在重置和迁移过程中删除db时,数据库的扩展名就被吹走(例如 rake db:drop db:create db:migrate )。 / p>

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

  class EnableExtensions< ActiveRecord :: Migration [5.1] 
def更改
enable_extension'uuid-ossp'
enable_extension'pgcrypto'
结束
结束


One of my Rails migrations uses a uuid as the primary key. The Postgres extension gen_random_uuid() should solve this issue, but I continue to get the error after installing the relevant extension (uuid-ossp).

解决方案

The issue was that the uuid-ossp extension was being blown away with the database each time I dropped the db as part of a reset and migration (e.g. rake db:drop db:create db:migrate).

The fix is to create a migration that's run before all other migrations which enables the relevant extension(s). Like so (db/migrate/0_enable_extensions.rb):

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

这篇关于Rails + Postgres迁移-为什么我会收到错误“ PG :: UndefinedFunction:ERROR:函数gen_random_uuid()不存在”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆