1

I have a scenario where i would like to create one new parent object with new child object using FLASK-REST-JSON API package . e.g

Scenario like to create a new Person with new computer in one POST method itself.

As per official documentation , we can create new person and new computer using separate POST and then link them together using relationships.However i would like to create both objects together in same POST method.

Please let me know is it possible ? If yes,any example would be great .

Thanks !

nja
  • 67
  • 2
  • 13

1 Answers1

1

Creating multiple resources with one request is not supported by JSON:API spec v1. There are some discussions to add support for such as an extension in upcoming v1.1: Introduce Atomic Operations extensions. There was an old extension called Bulk extension some time ago but that was experimental and the design of extensions has changed a lot by then.

I don't think any of these proposals / experiments have been implemented in Flask-REST-JSONAPI.

jelhan
  • 6,149
  • 1
  • 19
  • 35
  • Thanks for response. So creating multiple objects of same resource or creating multiple single object of different resource types both are not supported . Is this correct ? I feel this is very basic for any non trivial use case in real time scenarios where mostly one model does not represent a resource and resource can only be defined for multiple model ( db tables ) . Am i missing something here? Is there any example I can refer to know how to handle such scenarios ? – nja Oct 09 '19 at 20:43
  • There isn't something like models in JSON:API. A resource is defined by a resource object. It's defined by it's type and a unique ID. It may have attributes and relationships. You can't create multiple resources with one request neither of the same type nor of different types. This shouldn't be an issue for realtime scenarios. Sending multiple requests shouldn't make a performance issues for nearly all use cases. But it's a big limitation if you need atomic transactions. You may implement one of the proposed extensions in that case. But be aware that their are not part of spec yet. – jelhan Oct 10 '19 at 07:29
  • Maybe i am missing something here. Suppose we have different table UserPost and table Tags and its related schema UserPost and Tags. Typical scenario is to create UserPost with Tags together with single REST post or put API and not in separate post or put requests for UserPost and Tags – nja Oct 12 '19 at 06:50
  • @nja Your scenario sounds more like creating a UserPost with relationship to tags. That's possible. Just provide the relationship object with create request. But you can't create a new tag and a new UserPost at the same time. Or to put it the other way around: The tag must exist in order to reference it when creating a UserPost. – jelhan Oct 12 '19 at 11:30
  • As you have mentioned my scenario is related to creating new post with new tag together and we cannot do that as you have clarified. We can also first create post and then create tag and link to post using two different URL . However from front-end side then two request need to be send and may not be preferred by front-end developers .Is there recommended way or example to handle this kind of scenario – nja Oct 12 '19 at 14:14
  • I don't think there is a recommended way yet but the Atomic Operations extension has the highest chance to be become the official supported way to do it. But it might change on that way and might take some time. – jelhan Oct 13 '19 at 09:34