ash json patch with composite primary key
I have a resource with 2 primary keys
And this update action
And finally this json API route
patch :update
But how do I use it now? If I make this request
Then the wrong data is set as exposed. This is the query SQL query that is generated
In other words, the ID for the data is ignored and idk how to set it properly.
I also have this policy for what it's worth
Solution:Jump to solution
a new guide on the topic: https://github.com/ash-project/ash_json_api/blob/main/documentation/topics/composite-primary-keys.md
GitHub
ash_json_api/documentation/topics/composite-primary-keys.md at main...
The JSON:API extension for the Ash Framework. Contribute to ash-project/ash_json_api development by creating an account on GitHub.
19 Replies
π€ Sorry, not quite following. When you say the id is ignored what do you mean? Which part of that SQL is the problematic part? Not always easy to connect the dots on a fresh read, sorry π
There's a row in my db with UUID columns client_id 00000000-0000-0000-0000-000000000000 and data_id 3e8be57c-26ed-4eef-abe5-52526d52b982 and a boolean
exposed
column. The data_id is not used in the query.
The patch request is supposed to set the exposed to true in a specific row, but the generated query just selects a random row where the client_id is 00000000-0000-0000-0000-000000000000 and sets that to true. The data_id is hot present in the query
I think maybe I'm just misunderstanding how patch requests work in general with Ash json API π€π€ that is weird
Something seems wrong there
Maybe a bug in our parsing the composite primary key?
That definitely looks like a problem to me
Please reproduce and I will investigate.
I think I did it on top of the Ash Admin repo. But now should I make a PR to the admin UI repo or upload the repo on my account and just paste a link?
It's not exactly the same issue, maybe, but I think it's coming from the same place. Either way something is wrong with patch requests
I suppose I could also send patch files right here
@Zach Daniel
So apply these patches on top of
91331df9e5ed
of ash_admin
.
Run postgres through docker compose - docker compose up -d
.
Run the migrations to get the ExampleDomain
and Example.ExampleResource
- mix migrate
.
Run the server with iex -S mix dev
.
Go to /api/json/swaggerui
. You should see the get and patch endpoints.
Create some starting data:
Now try to edit one resource's exposed
to true
. For example, the first result returned in Swagger UI is this (see screenshot). Let's try to patch it. This is the curl call generated by Swagger UI
The result is success 200. But now when we look at our data in the Admin UI I expected the highlighted row to be true. Instead the third one is (see 2nd screenshot).This is the SQL query that was run for the request
It's not exactly what I talked about earlier, but it may stem from the same issue. The IDs provided in the patch request are not handled properly. Either that, or I'm gravely misunderstanding how I'm supposed to use this π
Is it possible for you to provide a full project that I can run? Reproducing piecemeal like this has cost me a lot of time in the past
You can just zip up your project where you are seeing this
Alright sure that works too
Although it would only cover the first step. You'd still need to do the steps from postgres down
π
Left the git history to make it clearer
It's only the top 4 commits
wtf
I guess no one has used composite primary keys with AshJsonApi
I don't understand how this hasn't been spotted before
π‘
Working on it
I have a fix, just working on some tests
Okay, I get it now
people would have been doing
route: "/:data_id/:client_id"
etc.
I will have to make this opt-in to make it non-breaking
We will now properly show an error on an unhandled input, and you can opt-in to behavior that sets a given path param to the composite primary key
@Sienhopist the resolution for this is a new option which is available in main
, please try it outSolution
a new guide on the topic: https://github.com/ash-project/ash_json_api/blob/main/documentation/topics/composite-primary-keys.md
GitHub
ash_json_api/documentation/topics/composite-primary-keys.md at main...
The JSON:API extension for the Ash Framework. Contribute to ash-project/ash_json_api development by creating an account on GitHub.
It looks like it does work based on a few tests. Wow you're fast.
In that case I only have one last question. This update wants us to provide the complete composite key in the url. However, can this be simplified for the caller of the API if the
^actor/1
already contains the client_id and the caller only wants to specify the data_id? Or should I make a generic action for this?Yes, you'd update the route
And add arguments to the action
argument :data_id, ...
And on the json api route
get ..., route: "/:data_id"
iirc
Double check the get route docsBut how do you get the client_id from the actor into the composite key?
change filter(expr(client_id == ^actor(:id))
Sorry for asking again, but I'm not sure how to get this to work and I also don't understand your example π
This is my update action
And this is my json API
But when I make a request like the following, I get an error that the data_id is missing.
I think I'm not doing something right
Actually never mind. While I would love to know how to actually do this, I realized if I went ahead with it then I wouldn't be able to reuse this route to allow admin users to make changes to other clients
πmakes sense.