Realm Sync - Optional relationships in Realm Objects

I have an object (.NET) as follows in my Realm Sync application:

public class Template : RealmObject
{
    [PrimaryKey]
    [MapTo("_id")]
    public int TemplateId { get; set; }
    public string TemplateDescription { get; set; }
    public Department Department { get; set; }
    public Example Example{ get; set; }
}

Where Department and Example are also Realm objects.
I want the Template class’ relationships to Department and Example to be optional i.e. so the template doesn’t need a reference to an instance of either object.

When I tried to add an object to the Realm Sync database without a reference to Department or Example, the corresponding MongoDB object had both Department and Example as Null objects which made the document invalid. how can I implement this object properly so the relationships are optional?

Hi, my name is Tyler and I am an engineer on the Realm Sync Team. I suspect what you are running into is a discrepancy between Sync’s interpretation of JSON schema and the RealmCloud UI’s interpretation of schema when clicking “validate schema” or using GraphQL, but let me know if I am misunderstanding.

A few questions that could enable me to help you a bit more:

  1. Could you send me either your app_id (the string that your app uses to connect to, this isnt something that is unsafe to send), or the link in your browser window when you are editing your app (also safe).
  2. Could you elaborate a bit on what was telling you that the document was invalid? Was it sync, the “validate collection” feature, accessing data through graphql, etc.

Thanks,
Tyler

Hi Tyler,

  1. The app id is textflowmobile-pbieg
  2. When I click the validate error on the realm schema page it says that the schema of the inserted entry is invalid. For more context here is the corresponding entry in the mongoDB:

{
“_id”:{"$numberLong":“123”},"
_partitionKey":“key”,
“Example”:null,
“Department”:null,
“TemplateDescription”:“Example”
}

It fails validation because of an InvalidTypeError for Example and Department (as they are inserted as null types when they are supposed to be int for the relationship references).

Clearly they need to be another type for a null object reference but I don’t know how that can be implemented.