Support Topics Documentation Slack YouTube Blog

1 to N Relationship with Users table

(Kasim) #1

I have created a new data table called “Music”. Then I created 1 to N relationship from my Users table. A user can have multiple music objects. When I use user.setProperty(“music”, music) it creates the relationship between Users and Music tables. But when same user tries to save another music object, then new music replace the old one and user still have only 1 music object. I know user.setProperty(“music”, music) is not the right command here but I couldn’t find what should I do here. What is the right approach here?

Thanx in advance!

(Mark Piller) #2

When you do this:

user.setProperty(“music”, music)

You assign ONE music object to ONE user. This is essentially one to one relationship between a user and a music. That relationship would be defined in the Users table (since the primary object is the one that HOLDS the related object, in this case it is your user).

If you assign a collection of music objects to user, it becomes a one to many relationship, between ONE user and MANY music objects. The relationship holder is still user.

However, if you assign a user TO your music object, then music holds the relationship, but the same rules I described above apply.


(Kasim) #3

Mark, thank you for explanation but I am still in dark here. My Users (primary object) holds 1:N relationship to Music table. In Users table I have a new column named “musics” and in Music table I now have “Users.musics” table. Now, when a user tries to upload a new music object, how do I update both tables?

(Mark Piller) #4

If your Users table holds a 1:N relation, then you’re assigning music to user in wrong way. You assign one object, but instead you should assign a collection of objects.

A relation in Backendless console is shown like this:</img>

which is different than the “child of” visualizer. The image below shows that OrderItem is a child of Order, but it is not a relation in the OrderItem table:</img>

(Kasim) #5

Good explanation, Mark! Thank you. Can you now show me a code snippet how do I retrieve my relationship objects for my BackendlessUser object and how do I put a new music object into BackendlessUser’s musics collection and update that user. That is my real problem. Can’t find the right code snippet for it (Android)


(Mark Piller) #6

Relation retrieval:

Updating object which has relations:

(Marcio Prudencio) #7

Hi Guys, little help please… I’m having a similar issue.

My table Users has a relation 1:N to table Orders, so users has unlimited orders. When a new order is created I set the relation of a new order to the column orders (1:n) on Users table all older orders assigned lose the relation and just the new one maintains assigned. How can I add a new order to my collection without lose all older related ones?

// Create relation in users table (already using :n)
Backendless.Data.of(“Users”).setRelation(userId, “orders:Orders:n”, [orderId])

Thanks in advance;

(Mark Piller) #8

Hi Marcio,

The answer is right in the documentation:

Specifically, see the following part:

To fix this, use the addRelation API instead of setRelations.


(Marcio Prudencio) #9

Thanks a million Mark, that solved the issue.