Ash FrameworkAF
Ash Frameworkโ€ข5mo agoโ€ข
19 replies
Lheau

Confusion with before_transaction timing and doing changes before manage_relationship

Hello, I am basically trying to use the data from a third party API call to create a related resource in manage_relationship.

Reading the doc I decided that it would make sense to call the API in a before_transaction hook. The docs is stating the following execution order for a create action:

2. **before_transaction**
4. **Action preparations/validations/change (in order of definition)**
...
7. before_action
...


But from what I'm seeing, my change (containing manage_relationship) is executing before my before_transaction.

Here is a reduced example I've quickly set up: (posted below)

If I just run for_create, my before_transaction doesn't get executed at all. (the doc for for_create/4 only mentions To have your logic execute only during the action, you can use after_action/2 or before_action/2.), which is already a problem as my manage_relationship depends on the data from the API.

If I comment out the crashing code and run a full Ash.create()!, I get the following logs:

iex(6)> Media |> Ash.Changeset.for_create(:add_to_collection, %{"third_party_id" => "id"}) |> Ash.create!()
[info] Change: manage relationship
[info] [private: %{[REDACTED]}]
[info] Change: before transaction
[info] Change: before action
[info] [data: %{name: "name", genre: "unknown"}, private: %{[REDACTED]}]


So my "action change" seems to be executed before the "before transaction" which isn't what I understood from the hooks execution order.

This leads me to 2 questions:

1. Is there something I'm missing with the execution order of the changes?
2. What would be the correct way of providing data changes to manage_relationship outside the transaction? What I've done for now is to include the manage_relationship inside the before_transaction hook, but then it is never executed until the action is actually ran, and then why not put everything in a normal change then?

Thanks ๐Ÿ™‚
Solution
Changes/validations run and then when you call the action before_transaction hooks are the first hooks to run
Was this page helpful?