gordoneliel
AshJsonApi: Create relationship when creating resource
I have a resource, eg Post, where the post has many Authors. When creating a Post, there should be at least one (1) Author attached to the payload. I know the json api v1 spec does not really outline creating multiple resources at once, but I figured we should be able to regardless?
What I’ve tried so far:
- Passing the relationship in the relationships section of the body - This fails with stack
[error] GenServer #PID<0.767.0> terminating
** (FunctionClauseError) no function clause matching in anonymous fn/2 in AshJsonApi.Request.relationship_change_value/1
(ash_json_api 0.33.1) lib/ash_json_api/request.ex:668: anonymous fn(:error, {:ok, []}) in AshJsonApi.Request.relationship_change_value/1
(elixir 1.15.7) lib/enum.ex:4387: anonymous fn/3 in Enum.reduce/3
- Passing the relationship into the
Postattributes when creating - This does not work, throws an invalid body because theAuthorattribute is not an attribute on the resource but rather a relationship.
I combed through the relationships section of Ash and AshJsonApi but cant seem to find anything related to creating multiple related resources.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 13 Posts
zachdaniel
The best way to go about this is to use
manage_relationshipi.eThen you can provide that as an input.
gordoneliel
I did try that with #1.
This got somewhere but seems to need an
idattribute in the relationship. When I add that none of the other attributes in the relationship make it to the changeset attributes underauthors.The other way was to put the relationship in the attributes
This did not work either, gave a invalid attribute error
zachdaniel
It should be done in the attributes when using that strategy IIRC. It’s been a while since I looked at that particular code. What invalid attribute error are you getting?
gordoneliel
Getting:
Expected only defined properties, got key ["data", "attributes", "authors"].This is my create action:
zachdaniel
Okay, I can see it now, yeah. It should be in the relationships, but the issue is that we are currently requiring
id, which doesn’t make sense in all contexts. I’ve just pushed something tomainto make those optional. Additionally, extra attributes have to go in the"meta"key of the relationship, i.e{type: "type", "meta" => {foo: "bar"}}. This is required by the spec, IIRC.gordoneliel
I think that would work! The json api spec for v1 is a little weird with this, I wonder if the upcoming v1.2 spec makes it easier to do this
gordoneliel
On a similar note, I have a situation where I have to create a
Comment.When creating a comment it should have the associated
Postit belongs to. I know we can get the data on a related resource for eg. GET/post/:id/comments, but when I try to send a ‘POST’ request to the same url, I get aroute not founderror.Question is, how do we post to a related resource? I tried just posting to the direct resource url, eg
/comments, but then I have to manually attach the relationship, and create two different actions on theCommentresource, one for when creating the comment through aPost(I would like to be able to create a comment when creating a new Post), and another when creating a comment after aPosthas been created.gordoneliel
Honestly not sure what the idiomatic way for JSONApi would be.
Should I have a
PATCHonPosteg/post/:id/comment, route: "/:id/contributors"(this is on post json_api routes), and an action on thePostresource that delegates creation of theCommentthrough the managed relationship? Or should it be aPOSTon/post/:id/commentsthat goes directly to theCommentcreate action?Example for reference:
zachdaniel
So, that part is actually still based on old behavior, and probably needs to be upgraded at some point. What it does is use the primary action and explicitly adds
Changeset.manage_relationship(instead of it being an argument in an action).So you can do this in routes:
gordoneliel
I do have that on the Post resource, but getting a
no route foundwhen hitting POST/post/:id/comments