Rails + Postgres migration - why am I receiving the error “PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist”?

左心房为你撑大大i 提交于 2019-12-22 03:43:46

问题


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).


回答1:


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



回答2:


Edge case answer:

Add the migration enabling the extension as stated above.

If you've previously had bigint id's and you're converting over to UUID, running rake db:reset db:migrate failed for me. Be sure to run rake db:drop db:create db:migrate as stated above!

If you get the error Environment data not found in the schema, run bin/rails db:environment:set RAILS_ENV=development.



来源:https://stackoverflow.com/questions/47064090/rails-postgres-migration-why-am-i-receiving-the-error-pgundefinedfunction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!