1 to N Relationship with Users table

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!

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.

Mark

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?

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:
http://support.backendless.com/public/attachments/078e5113ac39c522700173a0c4bd837e.jpg</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:
http://support.backendless.com/public/attachments/cb65fb97075b035204db1a6440b1f283.jpg</img>

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)

Regards,
Kasim

Relation retrieval:

https://backendless.com/documentation/data/android/data_relations_retrieve.htm

Updating object which has relations:
https://backendless.com/documentation/data/android/data_relations_save_update.htm

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;
Regards

Hi Marcio,

The answer is right in the documentation:
https://backendless.com/docs/android/data_relations_set_add_overview_android.html

Specifically, see the following part:

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

Regards,
Mark

1 Like

Thanks a million Mark, that solved the issue.

Regards;