hectorsq
App generated UUID primary key
I am working on a legacy database and the primary keys on the tables are a kind of UUID which we call OID.
This OIDs are 32 char strings generated by the application.
When I create a record, I generate a new OID and assign it to the corresponding field like this.
def create_document(attrs \\ %{}) do
oid =
Ecto.UUID.generate()
|> String.upcase()
|> String.replace("-", "")
attrs = Map.put(attrs, "oid", oid)
%Document{}
|> Document.changeset(attrs)
|> Repo.insert()
end
Since I have to do this for every table I am wondering if there is a better way.
Marked As Solved
axelson
Yes there is! I was looking into this the other day because I want to generate repeatable UUID’s for doc generation.
It works by setting the @primary_key module attribute :autogenerate option to a MFA. Here’s a short example:
defmodule MyApp.ExampleSchema do
@primary_key {:id, Ecto.UUID, autogenerate: {MyApp.OIDGenerator, :generate_oid, []}}
schema "example_schema" do
# ... schema fields
end
end
defmodule MyApp.OIDGenerator do
def generate_oid do
Ecto.UUID.generate()
|> String.upcase()
|> String.replace("-", "")
end
end
And you could even put your @primary_key definition in a module and then use MyApp.Data to bring it in.
Also Liked
hectorsq
axelson
Thanks! I’m glad it was helpful! I haven’t been able to make actual use of that code myself yet ![]()
Last Post!
axelson
Thanks! I’m glad it was helpful! I haven’t been able to make actual use of that code myself yet ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









