Realm Syncing relationship between two collection

Here’s my schema

export const articlemetaSchema = {
  name: 'articlemeta',
  properties: {
    _id: 'objectId?',
    __v: 'int?',
    authors: 'articlemeta_authors[]',
    catalog_id: 'objectId?',
    content: 'objectId?',
    createdAt: 'date?',
    description: 'string?',
    language: 'articlemeta_language',
    main_image_url: 'string?',
    origin_type: 'string?',
    pub_date: 'date?',
    publication: 'objectId?',
    sub_title: 'string?',
    title: 'string?',
    updatedAt: 'date?',
    url: 'string?',
  },
  primaryKey: '_id',
};

export const catalogpublicationSchema = {
  name: 'catalogpublication',
  properties: {
    _id: 'objectId?',
    __v: 'int?',
    _partition_key: 'string?',
    createdAt: 'date?',
    is_master: 'bool?',
    name: 'string?',
    updatedAt: 'date?',
  },
  primaryKey: '_id',
};

I want to setup a relationship between article_meta.publication and catalogpublication
but every time I try to do that I get an error that it is not validated

Requirement is to sync article_meta with publication object not the objectID

Just to confirm, you’re trying this with React Native?

Can you explain more about what you mean by “Realm Syncing relationship”? Is this MongoDB Realm Sync (and if so, what is your partition key?)

What code is failing, and what is the error?

No, I’m tryng this in Electron using nodejs sdk.

Given above are my schemas in which I want to define a relationship b/w articemeta.publication and catalogPublication._id

My partition key is _partition_key=‘catalog_id=some-ObjectId’

Realtionship which I definedimage

@Andrew_Morgan Did you get my problem?

Hi Shawn, I’ve not used Electron (or Realm Sync with the Node.js SDK) but I can take a look. Could you share the Realm schemas? Are you seeing the error when you try to add that relationship through the Realm UI or from somewhere else?

@Andrew_Morgan Here’s schemas:
ArticleMeta = {
“title”: “articlemeta”,
“properties”: {
“__v”: {
“bsonType”: “int”
},
“_id”: {
“bsonType”: “objectId”
},
“authors”: {
“bsonType”: “array”,
“items”: {
“bsonType”: “object”,
“properties”: {
“_id”: {
“bsonType”: “objectId”
},
“ambiguous_author”: {
“bsonType”: “bool”
},
“author”: {
“bsonType”: “objectId”
},
“createdAt”: {
“bsonType”: “date”
},
“updatedAt”: {
“bsonType”: “date”
}
}
}
},
“catalog_id”: {
“bsonType”: “objectId”
},
“content”: {
“bsonType”: “objectId”
},
“createdAt”: {
“bsonType”: “date”
},
“description”: {
“bsonType”: “string”
},
“language”: {
“bsonType”: “object”,
“properties”: {
“_id”: {
“bsonType”: “objectId”
},
“createdAt”: {
“bsonType”: “date”
},
“display_name”: {
“bsonType”: “string”
},
“language_id”: {
“bsonType”: “objectId”
},
“lcid_String”: {
“bsonType”: “string”
},
“name”: {
“bsonType”: “string”
},
“updatedAt”: {
“bsonType”: “date”
}
}
},
“main_image_url”: {
“bsonType”: “string”
},
“origin_type”: {
“bsonType”: “string”
},
“pub_date”: {
“bsonType”: “date”
},
“publication”: {
“bsonType”: “objectId”
},
“sub_title”: {
“bsonType”: “string”
},
“title”: {
“bsonType”: “string”
},
“updatedAt”: {
“bsonType”: “date”
},
“url”: {
“bsonType”: “string”
},
“_partition_key”: {
“bsonType”: “string”
}
}
}

Article= {
“title”: “article”,
“properties”: {
“__v”: {
“bsonType”: “int”
},
“_id”: {
“bsonType”: “objectId”
},
“active_search”: {
“bsonType”: “bool”
},
“article_meta”: {
“bsonType”: “objectId”
},
“catalog_id”: {
“bsonType”: “objectId”
},
“content”: {
“bsonType”: “objectId”
},
“createdAt”: {
“bsonType”: “date”
},
“flagged”: {
“bsonType”: “bool”
},
“owner_id”: {
“bsonType”: “objectId”
},
“rating”: {
“bsonType”: “int”
},
“read”: {
“bsonType”: “bool”
},
“status”: {
“bsonType”: “string”
},
“status_updated_at”: {
“bsonType”: “date”
},
“subscriptions”: {
“bsonType”: “array”,
“items”: {
“bsonType”: “object”,
“properties”: {
“_id”: {
“bsonType”: “objectId”
},
“createdAt”: {
“bsonType”: “date”
},
“origin_type”: {
“bsonType”: “string”
}
}
}
},
“updatedAt”: {
“bsonType”: “date”
},
“_partition_key”: {
“bsonType”: “string”
}
}
}

catalogPublication = {
“title”: “catalogpublication”,
“properties”: {
“__v”: {
“bsonType”: “int”
},
“_id”: {
“bsonType”: “objectId”
},
“_partition_key”: {
“bsonType”: “string”
},
“createdAt”: {
“bsonType”: “date”
},
“is_master”: {
“bsonType”: “bool”
},
“name”: {
“bsonType”: “string”
},
“updatedAt”: {
“bsonType”: “date”
}
}
}

SO I have already defined a relationship between article and articlemeta and it is working absolutely fine but when I tried to define relationship between articleMeta and catalogPublication No data is synced and I can not see any errors in the logs.
Thanks

One thing I noticed is that the collection name in the schema is “catalogpublication” but the relationship refers to “catalogpublications”

I generated the schema from realm using generate schema and also defined the relationship from realm UI

@Andrew_Morgan I generated the schema from realm using generate schema and also defined the relationship from realm UI and it automatically generates it.

One more thing, my catalogPublication and articleMeta does not have same partition value. Can it be a reason for not working?

tbh I’ve not yet used relationships with synced Realms, but I suspect that might be the issue as all synced objects in a Realm should be in the same partition.

@shawn_batra Your articlemetaSchema.publication field needs to point to the class definition you are trying to create a relationship to - in your case: catalogpublication

You can read more about this here:

@Ian_Ward Yes I have tried that but then I’m not receiving any data but with no error.

Could you please provide some help here because I’m stuck in this.
I want to define a relationship (explained above) because I want to sync only data which is required.

Note: partition Value is different b/w two collections. So is this causing the issue?

@shawn_batra The partition values must match for any objects/documents which you want related to each other. Otherwise you will get an error

I have shared the all my schema’s. Can you look n tell me if it’s possible or not.

I have defined relationship b/w articles and articlemeta and it’s working perfectly
I have noticed this thing only partition value was same in these two and it worked, now articlemeta and catalogpublication have different partition value and they are not working and I’m not receiving any error.

Related objects must have the same partition value in order to be valid relationships. Hence your error

Any solutions to this How Can I achieve this? Any workaround?

Yes you can use manual references which are not a realm reference but a string Id or something similar - similar to a foreign key concept - you can see this here in mongo’s documentation -

As well as me blathering on here :smiley:

1 Like

Yes we use manual references in Atlas.
Okay so According to use in my use case, I will have two local realm one will have article and articlemeta which are defined on partition value and other realm will have catalogpublications which is synced on other partition value

Now locally all data will get synced and I have to query manually from both the collections and map them togethter based on that reference (foreign key concept) Correct?

Now locally all data will get synced and I have to query manually from both the collections and map them togethter based on that reference (foreign key concept) Correct?

Yeah that’s correct. You would make a query that took a manual reference field in one realm and used it as a query in a different realm for a specific field. For instance you might have

Item.itemId

You then take the value of that field and create a query in a different realm which corresponds to an object field which could match