Id bigint in the DB Primary Column

Am using https://www.phoenixframework.org/ to create a Elixir Application.

While generating new module using mix phx.gen.json commands, I always find Database primary column is auto generated as id bigint NOT NULL DEFAULT. Is there any reason why Phoenix framework forces application to use id bigint as primary key?

Please delete this post, if its irrelavant

What else would you propose?

I suspect the unasked question here is “Why not use regular int? It’s smaller.

When your table is small, the difference between int and bigint is small. And with storage mostly being cheap nowadays, it does not matter that you use bigint for your table.

If your table starts growing big, you will need to then look at bigint anyway. If you started with int, you will need to do a migration and also keep monitoring that you do the migration early enough, before things start crashing. If you already started with bigint, you’ve got so much room that your table will practically never run out of IDs.

These combined make bigint a more practical choice for the majority of cases and thus a good default.

3 Likes

BTW, Phoenix does not force you to use bigint, it suggests this in its autogeneration, but you could easily change this be a uuid, by adding a config option.

config :your_app, :generators,
  binary_id: true

As seen in mix phx.gen.schema — Phoenix v1.7.14

4 Likes

There’s no forcing, it’s just the default which you can change.

1 Like