Schema is not the same as RealmObject

Hello. Can someone reproduce the Schema for my RealmObject? The schema that was produced when Developer Mode was ON didn’t got my field “meniu”. Also, what is the best way to have an array of RealmObjects as a field of one RealmObject?

class Restaurant : RealmObject {
    @PrimaryKey
    var _id: String = ""
    var userID: String = ""
    var name: String = ""
    var adresa: String? = null
    var telefon: Long? = null
    var meniu: List<Product> =  emptyList()
}

Can you post a link to your applications logs to see why the meniu field did not make it (it should have)

this is my class declaration:

class Product : RealmObject {
    @PrimaryKey
    var _id: String = ""
    var name: String = ""
    var category: String = ""
    var description: String? = ""
    var price: Float = 0F
    var imagine: String? = null
}

class Restaurant : RealmObject {
    @PrimaryKey
    var _id: String = ""
    var userID: String = ""
    var name: String = ""
    var adresa: String? = null
    var telefon: Long? = null
    var meniu: RealmList<Product>? = null
}
    suspend fun AddRestaurantTest() {
        withContext(Dispatchers.Default) {
            realm.write {
                val restaurant = Restaurant().apply {
                    this.userID="6360f373a6f3933e4c9cf6f3"
                    this.name = "name"
                    this.adresa = "location"
                    this.telefon = "07500000".toLong()
                    val listt = realmListOf(
                        Product().apply {
                            this.name = "cartofi"
                            this.category = "categorie"
                            this.price = "10.0".toFloat()
                            this.description = "daaaaa"
                        },
                        Product().apply {
                        this.name="rata cu cartogi"
                        this.category="carne"
                        this.price="25.0".toFloat()
                        this.description="daaaaa"
                    } )
                    this.meniu = listt
                }
                copyToRealm(restaurant)
            }
        }
    }

got this error now:

Product and Restaurant are empty collections

Fixed with this

    suspend fun AddRestaurantTest() {
        withContext(Dispatchers.Default) {
            val ob1 = Product().apply {
                this.name = "cartofi"
                this.category = "categorie"
                this.price = "10.0".toFloat()
                this.description = "daaaaa"
            }
            val ob2 = Product().apply {
                this.name="rata cu cartogi"
                this.category="carne"
                this.price="25.0".toFloat()
                this.description="daaaaa"
            }
            realm.write {
                val restaurant = Restaurant().apply {
                    this.userID="6360f373a6f3933e4c9cf6f3"
                    this.name = "name"
                    this.adresa = "location"
                    this.telefon = "07500000".toLong()
                }
                val myList=copyToRealm(restaurant)
                myList.meniu.add(ob1)
                myList.meniu.add(ob2)
            }
        }
    }

But only one single Product was added to the collection.
Also the meniu array field is empty on the Restaurants collection
image

Sorry, so have you resolved the issue? It sounds like your SDK model thought the meniu field was a list of links but the json schema in the UI only thought it was a list of strings. One part of it is that your previous schema for Product was incorrect but I get the sense you know that since you fixed it

the schema was produced by atlas with Developer mode ON. Why its a list of strings since I have a list of Products?

Ok, so I have done some changes to the model

class Product : EmbeddedRealmObject {
    var name: String = ""
    var category: String = ""
    var description: String? = ""
    var price: Float = 0F
    var imagine: String? = null
}

class Restaurant : RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId.create()
    var userID: String = ""
    var name: String = ""
    var adresa: String? = null
    var telefon: Long? = null
    var meniu: RealmList<Product> = realmListOf()
}

The Restaurant schema is good but I dont understand why the Product Schema was not produced. Also, Product collection was not created, probably because is embedded and is created inside Meniu field. I am right? Also, the data modeling is done how is supposed to be?

Hello @Ciprian_Gabor, :wave:

When you have developer mode ON, you can create all your schemas from your client side. I infer you are creating Android App, so you can create schema within the app itself, and when you run the app, the schema will be populated on the cloud.

All the fields may or may not show on the cloud if the data is not yet added for them. The schema of embedded classes will appear as and when you add data to those fields on the device.

If your data is not showing, either the schema needs to change or the way you are adding data. Could you share the cloud realm app id?

I look forward to your response.

Cheers, :performing_arts:
Henna

1 Like

This is the appId: afterfoodsuceavaapp-souma

Neither the collection or schema of Product(EmbeddedRealmObject) was added. But I can add/remove/update Products objects inside Restaurant.

Hey, if you look at your schema you will see that the schema for product is embedded within the schema for restaurant. In MongoDB / Realm there are two ways to model your data:

  1. Relationships: the two schema represent different collections. The primary keys (string in your case) are used to denote the relationship between the schemas for the field you define.

  2. Embedded: one schema “owns” the other and they both map to the same collection. Embedding documents is a powerful paradigm in MongoDB.

I would recommend reading through:

1 Like

Thank you for the info!

1 Like