How to insert objects to Atlas using Relationships with Realm in Kotlin?

I have created to RealmObjects: Category and Product, and I have created a one-to-many relationship, where one category has multiple products.
When I insert a product to Atlas database the category is not showing up. The products are not showing up either in Category collection.

These are my Realm Objects:

class Product: RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId.create()
    var name: String? = null
    var quantity: Int? = null
    var price: Float? = null
    var image: String? = null
    var shortDescritpion: String? = null
    var longDescritpion: String? = null
    var barcode: Long? = null
    var category: Category? = null
}

class Category: RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId.create()
    var name: String = ""
    var image: String = ""
    var products: RealmList<Product> = realmListOf<Product>()
    var company_id: Company? = null
}

These is product schema:

{
  "title": "Product",
  "bsonType": "object",
  "required": [
    "_id",
    "_partition"
  ],
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "_partition": {
      "bsonType": "string"
    },
    "name": {
      "bsonType": "string"
    },
    "quantity": {
      "bsonType": "long"
    },
    "price": {
      "bsonType": "float"
    },
    "image": {
      "bsonType": "string"
    },
    "shortDescritpion": {
      "bsonType": "string"
    },
    "longDescritpion": {
      "bsonType": "string"
    },
    "barcode": {
      "bsonType": "long"
    },
    "category": {
      "bsonType": "objectId"
    }
  }
}

This is category schema:

Relationship:

{
  "company_id": {
    "ref": "#/relationship/mongodb-atlas/bestock-database/Company",
    "foreignKey": "_id",
    "isList": false
  },
  "products": {
    "ref": "#/relationship/mongodb-atlas/bestock-database/Product",
    "foreignKey": "_id",
    "isList": true
  }
}

Body:

{
  "title": "Category",
  "bsonType": "object",
  "required": [
    "_id",
    "_partition",
    "name",
    "image"
  ],
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "_partition": {
      "bsonType": "string"
    },
    "name": {
      "bsonType": "string"
    },
    "image": {
      "bsonType": "string"
    },
    "products": {
      "bsonType": "array",
      "items": {
        "bsonType": "objectId"
      }
    },
    "company_id": {
      "bsonType": "objectId"
    }
  }
}

The database looks like this after I insert a category and a product:


Hi @Besart_H ,

It’s not clear from the information you provide, but, are you using Partition-based Sync, or Flexible Sync?

If the first is the case, the 2 records you report will never end up in the same partition, as they have different partition values, so the relationship won’t work.

If you’re using Flexible Sync instead, then the _partition field is redundant, of course

Hi @Paolo_Manna I am using partition-based Sync

Then, as explained before, the relationship can’t possibly work, as the Product document is in the product partition, while the Category is in the category partition, and relationships don’t work cross-partition.

To have it working, both objects should be in the same partition.

You may want to re-evaluate your data structure accordingly: some docs about different Sync modes, and Partition-based Sync may help here.

1 Like