Home > Blockchain >  Set default primary key type across entire rails app?
Set default primary key type across entire rails app?

Time:05-17

I will use uuid as the primary key type on some tables in a new rails app.

I see the occasional limitation like (at least out of the box) ActiveStorage can only handle attachments on either models that have bigint or uuid primary keys (not a mix of both).

For this reason, I think it's a cleaner approach to use uuid for every primary key.

Is there a global setting for new rails apps that can be set such that any models generated will automatically use uuid?

CodePudding user response:

Yes, set

config.generators do |g|
  g.orm :active_record, primary_key_type: :uuid
end

in config/application.rb.

If you have problems, enable pgcypto extension in postgres:

class EnableUuidExtension < ActiveRecord::Migration[6.1]
  def change
    enable_extension 'pgcrypto'
  end
end

Note: this must be the first migration to run!

  • Related