Check if changeset for create has upsert

Is it possible to check if a changeset action has the upsert?: true field applied?

Use case:

I have an action :register_with_password which doesn’t have any upsert set on it.

In my seeds, I want to add an admin user and upsert it in case it already exists.

To do that, I run it like this:

Core.Marketplace.Accounts.User
|> Ash.Changeset.new()
|> Ash.Changeset.for_create(:register_with_password, super_user_args, upsert?: true, upsert_identity: :unique_email)

Basically I add the upsert when calling Ash.Changeset.for_create.

Now, I have a change in that action that will check if the user already exists and return an error if it does. So what I want to do is check the changeset in that change to see if upsert?: true is set and, in that case, ignore it instead of returning an error.

Is that possible?

Hmm…it’s technically in private context, but it probably shouldn’t be private. changeset.context[:private][:upsert?]

1 Like