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.