moxley
moxley
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
Sounds good. Thanks @Zach Daniel !
15 replies
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
Or, not doing it in a migration is an option too.
15 replies
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
Doing raw INSERTs would be very difficult, because the record has multiple levels of nested, embedded values. The return_notifications?: true option seems like the best route for this.
15 replies
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
Okay, got it! I was adding return_notifications?: true to the Changeset.for_update() function. Adding it to API.update() makes sense.
15 replies
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
I would have to redefine several Ash resources into Ecto resources to avoid using Ash
15 replies
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
Most of the logic in the migration is adding records, using Ash resources, and Ash changesets.
15 replies
AEAsh Elixir
Created by moxley on 8/20/2023 in #support
Notifications warning in migration
I'm calling an Ash action from my ecto migration.
15 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
However, there seems to be the opposite issue when calling manage_relationship with type: :append:
# note_attrs = %{id: to_string(orig_note.id), body: "updated note"}
note_attrs = %{id: orig_note.id, body: "updated note"}

member
|> Ash.Changeset.for_update(:update, %{}, actor: session_member)
|> Ash.Changeset.manage_relationship(:notes, [note_attrs], type: :append)
|> GF.Ash.update!()
# note_attrs = %{id: to_string(orig_note.id), body: "updated note"}
note_attrs = %{id: orig_note.id, body: "updated note"}

member
|> Ash.Changeset.for_update(:update, %{}, actor: session_member)
|> Ash.Changeset.manage_relationship(:notes, [note_attrs], type: :append)
|> GF.Ash.update!()
Here, the note doesn't get updated when note_attrs.id is an integer. The note only updates when it's a string.
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
I modified my action to this:
update :delete_note do
argument :id, :string

change(fn changeset, _context ->
note_attrs = %{id: String.to_integer(changeset.arguments[:id])}

Changeset.manage_relationship(changeset, :notes, [note_attrs],
on_match: {:destroy, :destroy}
)
end)
end
update :delete_note do
argument :id, :string

change(fn changeset, _context ->
note_attrs = %{id: String.to_integer(changeset.arguments[:id])}

Changeset.manage_relationship(changeset, :notes, [note_attrs],
on_match: {:destroy, :destroy}
)
end)
end
I cast the string :id argument to an integer before calling manage_relationship.
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
That was it.
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
In the last test I posted, if I change this line, note_attrs = %{id: to_string(orig_note.id)} to note_attrs = %{id: orig_note.id}, it passes.
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
Okay, I think this has something to do with with converting between string and integer IDs
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
Calling manage_relationship doesn't delete the note either:
note_attrs = %{id: to_string(orig_note.id)}

_updated_member2 =
member2
|> Ash.Changeset.for_update(:update, %{}, authorize?: false,
actor: ctx.session_member)
|> Ash.Changeset.manage_relationship(:notes, [note_attrs], on_match: {:destroy, :destroy})
|> GF.Ash.update!()

member =
GF.Members.Member2
|> GF.Ash.get!(member2.id, authorize?: false)
|> GF.Ash.load!(:notes)

# Fails. Note is not deleted.
assert member.notes == []
note_attrs = %{id: to_string(orig_note.id)}

_updated_member2 =
member2
|> Ash.Changeset.for_update(:update, %{}, authorize?: false,
actor: ctx.session_member)
|> Ash.Changeset.manage_relationship(:notes, [note_attrs], on_match: {:destroy, :destroy})
|> GF.Ash.update!()

member =
GF.Members.Member2
|> GF.Ash.get!(member2.id, authorize?: false)
|> GF.Ash.load!(:notes)

# Fails. Note is not deleted.
assert member.notes == []
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
Here's the action:
update :delete_note do
argument :id, :string

change(fn changeset, _context ->
note_attrs = changeset.arguments

Changeset.manage_relationship(changeset, :notes, [note_attrs],
on_match: {:destroy, :destroy}
)
end)
end
update :delete_note do
argument :id, :string

change(fn changeset, _context ->
note_attrs = changeset.arguments

Changeset.manage_relationship(changeset, :notes, [note_attrs],
on_match: {:destroy, :destroy}
)
end)
end
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
Here's the test:
note_attrs = %{id: to_string(orig_note.id)}

_updated_member2 =
member2
|> Ash.Changeset.for_update(:delete_note, note_attrs, actor: ctx.session_member)
|> GF.Ash.update!()

member =
GF.Members.Member2
|> GF.Ash.get!(member2.id, authorize?: false)
|> GF.Ash.load!(:notes)

# Fails. Note is not deleted.
assert member.notes == []
note_attrs = %{id: to_string(orig_note.id)}

_updated_member2 =
member2
|> Ash.Changeset.for_update(:delete_note, note_attrs, actor: ctx.session_member)
|> GF.Ash.update!()

member =
GF.Members.Member2
|> GF.Ash.get!(member2.id, authorize?: false)
|> GF.Ash.load!(:notes)

# Fails. Note is not deleted.
assert member.notes == []
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
They both contain this piece of data:
relationships: %{
notes: [
{[%{id: "446"}],
[
ignore?: false,
on_missing: :ignore,
on_lookup: :ignore,
on_no_match: :ignore,
eager_validate_with: false,
authorize?: true,
on_match: {:destroy, :destroy},
meta: [inputs_was_list?: true]
]}
]
}
relationships: %{
notes: [
{[%{id: "446"}],
[
ignore?: false,
on_missing: :ignore,
on_lookup: :ignore,
on_no_match: :ignore,
eager_validate_with: false,
authorize?: true,
on_match: {:destroy, :destroy},
meta: [inputs_was_list?: true]
]}
]
}
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
I ran an IO.inspect at the end of the function above, and it looks exactly like the IO.inspect I applied when calling manage_relationship directly.
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
But it still doesn't delete the related record.
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
I also did it to the original code:
def delete_note(changeset, _context) do
note_id = changeset.arguments[:id]
note_attrs = %{id: note_id}

Changeset.manage_relationship(changeset, :notes, [note_attrs], on_match: {:destroy, :destroy})
|> dbg()
end
def delete_note(changeset, _context) do
note_id = changeset.arguments[:id]
note_attrs = %{id: note_id}

Changeset.manage_relationship(changeset, :notes, [note_attrs], on_match: {:destroy, :destroy})
|> dbg()
end
23 replies
AEAsh Elixir
Created by moxley on 7/19/2023 in #support
Using manage_relationship to delete a related record
I set on_match: {:destroy, :destroy}.
23 replies