Define schema in mongo realm

I have two table User and User role. where I want to add relation between these two table. I want to add Userrole into user table. here is my schema in mongo realm schema.

After doing this…when I query the data it gives me empty array of user. it is confusing how the schema will look like if i want to apply realm sync and how data population will done.
your help will be appreciated.!! Thank You in advance.

The bsonType of User.UserRole should be objectId - it should match the _id type of the object you’re trying to create a relationship to.

Then, you should go to the Relationships tab (right next to Schema) and create a new relationship from User.UserRole to UserRole._id.

1 Like

I did as u have explain. It’s working!!.. but here i have one concern… like when I add records into userTB at that time I need to pass whole object of userRole…just UserRoleID is not sufficient as in normal MongoDB insertion.

Is there any alternative…or this is how mongo realm works???
When I just Add UserRoleID instead of whole obj…it is throwing an error for missing value

Realm has a different data model than MongoDB - it offers relationships as first class concept and is generally geared toward mobile development where ease of use is more important than scaling to support terabytes of data.

So yes, you need to assign the entire object - if you know the UserRoleID, you can look up the UserRole first and then assign it (will need to adapt that to your language of choice, but should look like):

var myUser = new User();
var role = realm.Find<UserRole>(userRoleId);
myUser.UserRole = role;
1 Like

N what if I’m inserting new record in ‘User=Id’ partition and the role I want to assign is in the ‘Public’ partition.
I’m opening public partition to get particular role.
then I’m inserting new user and assigning myUser.UserRole = role

it is throwing an error… like the role that I’m assigning from public partition is from other realm

How to active this scenario…where i need some depended object from other partition.

As per the documentation, this is not possible. You’ll need to either duplicate the user roles in all partitions, or find a different partitioning strategy that allows you to do what you want.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.