Error when using before_action hook to set required relationship attribute

I'm using a before action to lookup and set the :state_id by the state's name or code. However, the change doesn't seem to be running as I never see the Logger.debugs and I get changeset errors that :state_id is required. Have I misconfigured this hook? Action
create :create do
primary? true
upsert? true
upsert_identity :unique_google_place
upsert_fields {:replace_all_except, [:id, :state_id, :inserted_at]}
skip_unknown_inputs :*

change __MODULE__.Changes.SetState,
where: [present(:google_state), absent(:state_id)]
end
create :create do
primary? true
upsert? true
upsert_identity :unique_google_place
upsert_fields {:replace_all_except, [:id, :state_id, :inserted_at]}
skip_unknown_inputs :*

change __MODULE__.Changes.SetState,
where: [present(:google_state), absent(:state_id)]
end
Change Module
defmodule Boring.Google.Place.Changes.SetState do
@moduledoc false
use Ash.Resource.Change

require Logger

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
Ash.Changeset.before_action(changeset, fn changeset ->
state = Ash.Changeset.get_argument_or_attribute(changeset, :google_state)
Logger.debug("Setting state for #{state}")

opts = Ash.Context.to_opts(context)

case Boring.Locations.get_state_by_name_or_code(state, opts) do
%Boring.Locations.State{} = matching_state ->
Ash.Changeset.force_change_attribute(changeset, :state_id, matching_state.id)

_ ->
changeset
end
end)
end
end
defmodule Boring.Google.Place.Changes.SetState do
@moduledoc false
use Ash.Resource.Change

require Logger

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
Ash.Changeset.before_action(changeset, fn changeset ->
state = Ash.Changeset.get_argument_or_attribute(changeset, :google_state)
Logger.debug("Setting state for #{state}")

opts = Ash.Context.to_opts(context)

case Boring.Locations.get_state_by_name_or_code(state, opts) do
%Boring.Locations.State{} = matching_state ->
Ash.Changeset.force_change_attribute(changeset, :state_id, matching_state.id)

_ ->
changeset
end
end)
end
end
Solution:
GitHub
Error when using before_action hook to set required relationship at...
Versions erlang 27.3.4.1 elixir 1.18.4 ash 3.5.12 ash_postgres 2.5.22 ecto 3.12.5 ecto_sql 3.12.1 Originally found and reported in Discord Support on 6/16/2025 and tried upgrading the latest versio...
Jump to solution
13 Replies
ZachDaniel
ZachDaniel•4mo ago
🤔 have you tried removing the condition? Like the where? That would tell you if its an issue w/ the predicate or not
Chaz Watkins
Chaz WatkinsOP•4mo ago
I did, but let me try again real quick yeah, same thing
ZachDaniel
ZachDaniel•4mo ago
🤔 how are you calling it?
Chaz Watkins
Chaz WatkinsOP•4mo ago
through the code_interface as a bulk action
ZachDaniel
ZachDaniel•4mo ago
You're on the latest version of everything? We had a bug around this that we fixed
Chaz Watkins
Chaz WatkinsOP•4mo ago
|> Boring.Acquisitions.create_opportunity(
Keyword.merge(opts,
load: [:assigned_to_org?, :google_place],
bulk_options: [return_stram?: true, return_errors?: false],
authorize?: false
)
|> Boring.Acquisitions.create_opportunity(
Keyword.merge(opts,
load: [:assigned_to_org?, :google_place],
bulk_options: [return_stram?: true, return_errors?: false],
authorize?: false
)
I just ran igniter.upgrade. Let me blow away _build and deps to see if that helps
ZachDaniel
ZachDaniel•4mo ago
check w/ mix hex.outdated too just in case unless you're on branches then I think we have an open issue about this I'm not sure if igniter.upgrade does git dependencies Try mix deps.update
Chaz Watkins
Chaz WatkinsOP•4mo ago
everything is already up to date. Still happens after reinstalling everything I'm using all hex versions
ZachDaniel
ZachDaniel•4mo ago
😢
Chaz Watkins
Chaz WatkinsOP•4mo ago
the only other config I have for the upsert is this. Maybe an issue? saying not to replace state_id?
ZachDaniel
ZachDaniel•4mo ago
It is probably a bug. Can you push a test to your app? or make a repro?
Chaz Watkins
Chaz WatkinsOP•4mo ago
yeah, I'll open an issue and see if I can do a repro I using a work around with a no attributes relationship for now. Have a hard deadline to get some new features out by tomorrow night. Once I get that done, I’ll create an issue and look into the root cause. I added an issue in the ash repo for tracking. Still haven't had a chance to make a reproduction, but will add it to the issue when I can.
Solution
Chaz Watkins
Chaz Watkins•4mo ago
GitHub
Error when using before_action hook to set required relationship at...
Versions erlang 27.3.4.1 elixir 1.18.4 ash 3.5.12 ash_postgres 2.5.22 ecto 3.12.5 ecto_sql 3.12.1 Originally found and reported in Discord Support on 6/16/2025 and tried upgrading the latest versio...

Did you find this page helpful?