I did ask a related question some time ago and now i face some more issues about that.
This piece of code no longer works
defp upsert_via_data_layer(changeset, identities) do
changeset.resource
|> Ash.DataLayer.data_layer()
|> Code.ensure_loaded!()
Ash.DataLayer.upsert(changeset.resource, changeset, identities)
end
When i execute it i get an object with all fields set as nil.
Could the problem be that some of the fields that i want to write have writable? false
?
How could i bypass this problem?
1 Like
Right, so we don’t expect this sort of thing to be done TBH. In this case, a feature was added to prevent data layers from selecting unnecessary fields. I will tell you how to solve the immediate issue, but I’d advise not doing it the way you are doing it in general.
defp upsert_via_data_layer(changeset, identities) do
changeset.resource
|> Ash.DataLayer.data_layer()
|> Code.ensure_loaded!()
changeset = Ash.Changeset.set_action_select(changeset)
Ash.DataLayer.upsert(changeset.resource, changeset, identities)
end
Thank you
! That works.
Right, so we don’t expect this sort of thing to be done TBH
You mean bypassing the actions?
So the correct way to seed would be to specify in every resource a new action?
If so how would i be able to call the relative action in a general way like this Ash.DataLayer.upsert(changeset.resource, changeset, identities)
?
Have you looked into Ash.Seed
? Ash.Seed — ash v3.4.32
I tried it out last evening but didnt find a way to do an upsert with it.
Ash.Seed.seed!
fails because i have an identity on the email.
Ash.Seed.update!
fails too because i could not have anything on the db.
This is what managed to work for me
If you think that an upsert! function is something that could come in handy like in my case i can work on a quick pr to add this method to the Ash.Seed module
I would accept an upsert function in Ash.Seed yes 
1 Like
Awesome, i made a pr regarding this. You can find it here!
2 Likes