MarkMekhaiel

MarkMekhaiel

Ecto migration with default value from another column in same table

Hi,

I’ve a few million records, and I’m trying to add a column, uuid, to their table that has a default value of each row’s id field. Is this possible? And if not, would it be possible to give it a default value that increments?

My aim is to have unique values in the new UUID column so I can then add a unique index.

Thanks in advance.

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Probably it will be best to add the column with a default value of gen_random_uuid() and then that way they are actually UUIDs.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Smaller size is the first order impact (the string representation is twice as big as the binary representation) but at large scale the smaller size directly translates into improved query performance because the indices are smaller and thus faster to traverse.

MarkMekhaiel

MarkMekhaiel

Thanks for your help. I ended up with this which works as intended:

  add :uuid, :string, null: false, default: fragment("gen_random_uuid()")
marciotrindade

marciotrindade

@MarkMekhaiel If it is a table with some records (a lot of them), my recommendation is to do it with more than 1 migration to avoid having your table locked.

My suggestion is:

  1. Create a new UUID column that does not have a default value and can be null.
  2. Run a script to generate value for empty records in a batch of some records until all of them are updated.
  3. In the same migration (using the same transaction, yes Postgres uses transactions for DDL) run your script again and change the UUID column to be not null and having gen_random_uuid() as a default value.

That way you can avoid changes in a table with a lot of records and lock the table during the generation of those values.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would strongly suggest using the binary_id type which will much more compactly store the underlying UUID.

MarkMekhaiel

MarkMekhaiel

Thanks for the advice, I ended up copy and pasting from Backfilling Data · Fly - batching every 2500 and throttling inbetween with a temp table to store the ID’s of completed records

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement