How to use UUIDv7 with Ecto and PostgreSQL?

This is how I currently use binary IDs in my Phoenix apps.

Generate the app:

mix phx.new path/to/app --binary-id

Configure Ecto and migrations:

config :app_otp_name,
  namespace: App,
  ecto_repos: [App.Repo],
  generators: [binary_id: true]

config :app_otp_name, App.Repo,
  migration_primary_key: [name: :id, type: :binary_id]

Configure Ecto schemas:

@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id

How can I configure this to use PostgreSQL-generated UUIDv7 values instead of UUIDv4 values?

Hi @mtdelahaye

According to the Ecto 3.14 documentation: Ecto.UUID — Ecto v3.14.0

you can achieve it as follows:

use Ecto.Schema

@primary_key {:id, Ecto.UUID, autogenerate: [version: 7]}

I hope you find it useful.

Regards.