Is it possible to "create" associated resources at the same time as the main resource?
I'm hitting a post endpoint for a resource that has a relation. To use your example, it would be much like posting a user that has data for two chocolates.
I notice that the resource user gets created, but not the associated chocolates.
I am now doubting that this is even possible and would love it if you could confirm one way or the other. When I look at your example for a post, I see that there is no payload for the association:
https://github.com/manyminds/api2go/blob/master/examples/crud_example_test.go#L60
On my end, I've debugged enough to see that when SetToManyReferenceIDs is called, the IDs that are passed in are correct in number (there are two associations and there are two IDs), but they are set to zero. This makes sense because at this point create hasn't been called yet.
When create does get called, I inspect the obj and the array for the association (which would be chocolates) is empty. If it wasn't empty I could grab the data and save it to the db.
I have implemented all the interfaces to make associations work, at least I'm pretty sure I have.
Hi,
this should work, however the example is very basic and I haven't implemented that in the code. I will try to add this feature in the example and extend the example tests to verify that this really works when implemented.
Hey @wwwdata I'd appreciate that. Especially since the alternative is to create one resource then make individual api calls for each relation.
I think I was very diligent in making sure I was doing everything correctly, so I'm looking forward to the results of your test.
I found the problem in our examples. As expected I just made a mistake. When reference IDs are in the payload, the SetToManyReferenceIDs and SetToOneReferenceID depending on the reference should be called in your resource model. Depending on what you do there, this should be working.
Ok I will make that change. But still, I noted that in the updated example the ID is 1 (https://github.com/manyminds/api2go/pull/271/files#diff-f6c08d9b83fa8e3e50949d71d299271dR223). But it can't be 1 in real life because it hasn't been created yet. So it will always be zero, correct?
ah, now I understand what you mean. You want to create the user and new referenced data in one request. So one user and 2 chocolate datasets. Hmm. Are you trying to send the chocolates in the included part of the JSON?
I think there also is no official example in the jsonapi.org spec, right? They only have examples with reference IDs.
That is precisely what I'm trying to do.
Yes, the chocolates are in the included part of the JSON. That's actually how api2go spits it out after the struct (user) with the slice of associated struct (chocolates) is marshalled.
api2go cannot do that for now. Unmarshaling is only working with IDs. One other problem we have is how we manage the IDs of the new chocolates. The included section can include many references. There are also scenarios possible where the same included types can belong to different relationships.
We should look a bit more into the jsonapi specification if anything is specified how to do this properly or if something is in the works that we can early adopt. Did you find anything in their documentation how to correctly send payload to create relationships along the the main object @carlisia ?
Hm. This is interesting. Looking now I see that they specify how to do a post with an association if it is something like, posting a chocolate with an association back to the user: http://jsonapi.org/format/#crud-creating
Nothing yet on how to do it the other way around.
This feature seems to be planned for 1.1 http://discuss.jsonapi.org/t/json-api-weekly-meeting-june-22nd-2015/23 but we only support 1.0 at the moment..
This seems like a common problem. I found several folks complaining about the lack of atomicity in create calls to JSON API. My solution was the same as @carlisia, I'd like to post the main object, and it's related object in the included array.
Like @carlisia i think the simple solution would be to include the entire object (in raw format) as part of the request object (or the object, object) so that I could at least implement this myself if I wanted to.
It seems that this is the issue where this feature is being discussed: https://github.com/json-api/json-api/issues/1089
Wondering if there has been some update with resolving this issue?
unfortunately not, you should watch this issue https://github.com/json-api/json-api/issues/1089
too bad, thanks though