Delete Queries.
How does delete queries work? like if i want to fetch a record based on and id or maybe two columns and want to delete that record?
33 Replies
I tried doing this.
So you can look up a record however you want and destroy it, i.e
Api.get!(..) |> Api.destroy()
but right now we donβt have destroys with queries. Its coming soon.and i had a lot of help from ash-hq repo. Thank you so much for your hard work ππ»
If you want to delete from a query, you can do
MyApp.Accounts.TeamJoinedUser |> Ash.Query.filter(β¦) |> Ash.Query.data_layer_query()
That will get you an ecto query that you can pass into Repo.delete_all
Is this okay?
it says module repo is not defined.
It would be your apps repo module
yeah i know but usually we have this code in our repo.ex
but for this i have this in my repo.ex
You don't need the
adapter
line w/ AshPostregres.Repo
In your examples, you've got MyApp.Repo
in the query, and here AppName.Repo
. I get that you may be trying to hide private code, but it seems like the issue is probably not using the right module name?my module to repo.ex is
AppName.Repo
and i have aliased it at the top like this alias AppName.Repo
but i am getting the below error
yeah just hiding the private infoDo you have the repo in your
config.exs
? config :app_name, ecto_repos: [AppName.Repo]
?yes
Can i give two params here?
I don't know what is wrong here.
Is there any other way that i can fetch the data?
basically i want to fetch data from TeamJoinedUser based on team id and user id after getting that one record delete that record.
The easiest way IMO would be to create a custom action:
this would be in TeamJoinedUser?
That will expect exactly 1 result, so you will want to ensure you have an identity set up on those two fields to ensure uniqueness by that trait.
and then how will i call it?
Yeah.
I would use
code_interface
and add it to TeamJoinedUser
, then you can just do TeamJoinedUser.get_by_team_and_user
.and this will be in
in it or outside?
The custom action goes inside
actions do
, after defaults ...
okay and i defined both of them.
To clarify what's going on in the DSL:
is shorthand for
The syntax is
[type of action] [name of action] do
TeamJoinedUser.get_by_team_and_user(team_id, user_id)
It will be called like this? in the live view?Yes. But you'll want the
!
if you're going to pipe it into destroy.
TeamJoinedUser.get_by_team_and_user!(team_id, user_id)
and defined this in team_joined_user.ex
MyApp.Api
This will be like this or MyApp.Accounts.Api
?Depends entirely on how you set it up.
You'll use whichever Api has this resource in its registry.
got it.
Let me try to implement it
@frankdugan3 it gives me an error
even though i have aliased it
How are you calling it?
Also, do you have the API in
config.exs
?
in my liveview i am calling it like this
TeamJoinedUser.get_by_team_and_user(team_id, user_id)
yes
but it is defined like AppName.Accounts
OK, it's hard to debug when you don't provide the module details. If your Api is defined
defmodule AppName.Accounts do
, then you need to refer to it that way, not as AppName.Accounts.Api
.I can provide details that you need. What should i send?
The Api module to start with.
This was the issue.
Thank you so much @frankdugan3 ππ»