[Rest API] Create an Object with relation in a single request

Hi there

I have 2 tables; Classes and Students and classes have students (one to many)

So what i want is when i create a new student i also assign it to its owner class in a single call.

How can i achieve that?

Hello!

In order to do it you should first get objectId for Class object which you want to be the parent for the student. Then the following request would update Class object and create new student with name “New Student”:

curl 
-H application-id:<application-id>
-H secret-key:&lt;REST-secret-key&gt;
-H Content-Type:application/json
-H application-type:REST
-X PUT 
-v [url=http://api.backendless.com/v1/data/Class/]http://api.backendless.com/v1/data/Class/&lt;class-object-id&gt;[/url] 
-d '{"students":["___class":"Student", "name":"New Student"]}'

All previous students would be saved in this case, so, the new student would be added to the list.
best regards,
Alex

Thanks :slight_smile:

Another scenario

How can i assign 1 object to two owners in 1 call?
For example, there are 3 tables, Patient, Doctor, Prescription, and whenever a new prescription is created i want to attach this 1 prescription to both patient and doctor?
Please advise

There is no one-step way for this case.

I’d better change schema so Prescription would have two one-to-one relations: for example, “doctor” referencing “Doctor” object and “patient” referencing “Patient” object. In this case you’ll be able to create Prescription and set related objects in one request:

curl
-H application-id:<application-id>
-H secret-key:&lt;REST-secret-key&gt;
-H Content-Type:application/json
-H application-type:REST
-X POST
-v [url=http://api.backendless.com/v1/data/Prescription]http://api.backendless.com/v1/data/Prescription[/url]
-d '{
"prescriptionDescription":"description",
"doctor":{"___class":"Doctor", "objectId":"&lt;doctor-object-id&gt;"},
"patient":{"___class":"Patient", "objectId":"&lt;patient-object-id&gt;"}
}'

best regards,
Alex

That is doable But then how can i get all the prescriptions of a single doctor?

Suppose you have table “Doctor” and table “Prescription”. Let’s assume there is a relation column defined in Doctor called “prescriptions”. The relation is one-to-many. Loading all the prescriptions for a doctor would work as defined here:
https://backendless.com/documentation/data/rest/data_relations_retrieve.htm

But then how can i look from prescription to doctor like a graph

For example i get a prescription and now i want to see who prescribed it?

You can do it using where clause “doctor.objectId=’<doctor-object-id>’”. I assume that relation name is “doctor”.Here is a request sample ( where clause is encoded ):

curl
-H application-id:<application-id>
-H secret-key:&lt;REST-secret-key&gt;
-H Content-Type:application/json
-H application-type:REST
-v [url=http://api.backendless.com/v1/data/Prescription?where=doctor.objectId=%3Ddoctor-object-id%3D]http://api.backendless.com/v1/data/Prescription?where=doctor.objectId=%3Ddoctor-object-id%3D[/url]
-X GET

This query sent to the Doctor table would do it:

prescriptions.objectId = 'prescription-object-id'

Hi Alex,

I’m getting following error for above request.

“Relation type update is prohibited - property refills must relate to table Users but not Refills”

Where User is SignedIn User and have refills (one to many relationship) with Refills Column.

Please advise