AshPhoenix.Form.submit with upsert?

I have a model that uses email as the primary key:

attribute :email, :string do
  primary_key? true
  allow_nil? false
  constraints max_length: 255,
              match: ~r/@/
end

In my registration form, I want to upsert: if the email address exists, just overwrite the other fields. I was hoping that something like this would work:

def handle_event("save", %{"form" => params}, socket) do
  case AshPhoenix.Form.submit(socket.assigns.form,
    upsert?: true,
    upsert_identity: :email,
    params: params) do
      {:ok, _lead} ->
        ...
      {:error, form} ->
        ...
    end

Two questions:

  1. Is it possible to do an upsert, directly from AshPhoenix.Form.submit?
  2. If upsert? and upsert_identity are invalid keys, why doesn’t this throw an error?

Sorry, I’ve been looking at the book and the docs, and haven’t been able to figure this out… Thank you!

Pass the upsert? and upsert_identity keys when creating the form, not when submitting it :slight_smile:

As for why you aren’t getting errors, there are a few places that unfortunately don’t validate the options given to them, primarily because of some old implementation that just hasn’t been updated. In the case of forms, there are “hidden” options that are passed only internally when working with forms, and so we didn’t write the options validator. It should happen :slight_smile:

3 Likes