Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 649
user3006381 Avatar asked Nov 01 '17 21:11

user3006381


1 Answers

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
like image 135
user3006381 Avatar answered Nov 12 '22 07:11

user3006381