Home > Software engineering >  How to insert objects to Atlas using Relationships with Realm in Kotlin?
How to insert objects to Atlas using Relationships with Realm in Kotlin?

Time:08-24

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 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
}
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
}

This 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:

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

Attributes:

{
  "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:

enter image description here

enter image description here

CodePudding user response:

There may be a number of issues causing this - sync could be stopped on the server or some other error. Checking the logs in the Realm Website console is always a good first step

I do notice that your two objects exist in different partitions; the Category object is in the category partition and the Product object in in the product partition.

That won't work as is - they would need to be in the same partition for the relationship to exist.

  • Related