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: