Adding ULID as an additional Primary Key option

Hi,

I want to use ULIDs (GitHub - ulid/spec: The canonical spec for ulid) as a primary key option. Does anything speak against adding this directly to ActiveRecord? I understand that currently, only UUID and Integers are implemented.

Any suggestions?

Kind regards, Tim

You can use create_table :sites, id: :string do |t| in the migration when creating the table and then set the id to the ULID in a before_create callback.

The ulid-rails gem allows you to have ULID primary keys in Rails. Just have a migration like this:

def change
  create_table :users, id: false do |t|
    t.binary :id, limit: 16, primary_key: true
    ...

and then in your corresponding model, have this:

class User < ApplicationRecord
  include ULID::Rails
  ulid :id, auto_generate: true
  ...