Forcing generation of Ecto autogenerate fields

Hi folks, do you know how can I force the autogeneration of ecto schema fields? I’m using Ecto Schema + MongoDB and I have the following schema:

  embedded_schema do
    field :identity, :string
    field :password, :string, redact: true
    field :password_confirmation, :string, redact: true

    timestamps()
  end

Then I call

  def validate(attrs) when is_map(attrs) do
    attrs
    |> then(&changeset(%__MODULE__{}, &1))
    |> apply_action(:insert)
  end

But that does not generate the id nor the timestamp fields. Is there a way of doing it?

Those fields only get autogenerated/updated when you make a call to insert or update them right before the fields are dumped to be inserted/updated
It doesn’t look like there’s a public-facing function for it

It doesn’t look like it would be too difficult to write your own if you’re comfortable touching the internals

1 Like