mtdelahaye

mtdelahaye

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?

Marked As Solved

mcsan

mcsan

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.

Last Post!

AstonJ

AstonJ

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement