Support Topics Documentation Slack YouTube Blog

Relational tables Post (REST)

(Oktay Isiktekiner) #1

Hi, I have 2 tables. 1 table has people the other has boxes. Box table contains a column related with Person table. 1 box belongs to 1 person and 1 person may have more than 1 box. I couldn’t find a way to fill person column when I post a new box. I tried different post data. One of sample:

	"BoxId": "eaf306de-279f-4e61-861b-9d6220715f97",
	"Quantity": "2",
	"Person": {
		"objectId": "704ED266-B94A-A8DB-FF2A-A82164B5B100",
		"___class": "Person"

It creates a new box but relational column Person is empty in backendless data browser. I have more columns in Person table. I want to give a relational id only (object id) to join tables. Because person is not updated. I don’t want to send every column for Person in post data. In application I have person’s objectId.
In examples it writes new address for a contact for example. In my case it shouldn’t add new Person. I want to use existing person for a new Box.

(Sergey Chupov) #2

Hello Oktay,
Would be useful if you provide us with your current schema in Backendless first. From your description, I suppose you need to have a one-to-one relation column from Box to Person table, and one-to-many relation column from Person to Box table. Although, depending on what your application needs you might suffice with only one of them.
With those two relation columns you’ll need from 2 to 3 calls to save a new Box related to existing Person: the first one is actually saving a Box entity (doc) with its own columns, not relation ones, and the next ones are setting relations from Box to Person and from Person to Box (doc) (if you need that backward relation).

(Oktay Isiktekiner) #3

Hi, this is just an example to understand how it works with REST. I couldn’t run .NET SDK on Xamarin mobile. Compiling gives error with backendless sdk reference. Because of this I turn to REST.

I used to work with relational databases. I may need to change structure. Should I put Box column (1:n) in Person instead of using person’s objectId in Box table?

(Sergey Chupov) #4

It doesn’t matter much, actually - you’ll be able to do querying in both directions with either one of the columns, and keeping both can be harder to maintain.

Assuming your background with relational databases, I guess it would be more convenient to have only the Box-to-Person one-to-one relation.
This way saving a new Box with a relation to existing person is a two step process:

  1. Save a box (no relations)
  2. Create a relation between the Box and the Person - this request only requires you to send the objectId values of both