Implementing a form of join query on a realm db without having a predefined relationship in structure

12,285

While Realm is a NoSQL database, it still has a schema, so you cannot return multiple classes in the same query.

What you are asking for could be solved by having an abstract superclass with the shared fields. That has not been implemented yet, but you can follow progress on it here: https://github.com/realm/realm-java/issues/761

Also, JOINS doesn't exist in Realm as it is not a relational database, but more like an graph database. References to other objects are just like object references. You can read more here: https://realm.io/docs/java/latest/#relationships

Share:
12,285
Realz
Author by

Realz

Updated on June 15, 2022

Comments

  • Realz
    Realz almost 2 years

    I have used SQL engines and some noSQL engines as well like indexdb and it's possible to scrape data across several tables without defining foreign keys or something.

    My question is, is it possible to make a query to scrape data across objects tables in Realm without defining any special relationship in the structure? To express my self better, I'm going to post sample codes of what I'm wanting to achieve with Realm so you could help me.

    Implementation using dexie, an indexdb wrapper

     db.quote_items.where('quote_id').equals(quote_id).then(function(a){
           db.products.where('id').equals(quote_id.product_id).then(function(){
               list.push({'id': a.id, 'product_name':a.product_name, 'product_code': a.product_code, 'quantity':a.quantity,  'tax':a.tax, 'unit_price':a.unit_price, 'val_tax':a.val_tax, 'discount_val':a.discount_val, 'gross_total':a.gross_total, 'details ':b.details   });
    
           }).catch(function (e) { console.log(e); alert("Sorry, Something went wrong"); })
       }).catch(function (e) { console.log(e); alert("Sorry, Something went wrong");})
    

    Implementation in mysql

        SELECT quote_items.id AS id, quote_items.product_name AS product_name ...... FROM quote_items, products WHERE quote_items.quote_id = quote_id AND products.id = quotes_items.produc_id 
    

    Expected implementation in Realm.io for Android

      RealmResults result = realm.where(quote_items.class)
                .equalTo("quote_id", quote_id).equalTo("quote.product_id", quote_id).equalTo("product.product_id", "quotes.itemkey").findAll()
    
  • Realz
    Realz almost 9 years
    Thank you Christian, I appreciate that you.The situation is that the data for the subclass object being related to the main class in the model might be available at a different time from the creation or insertion of the main object. So this makes it impossible to actually relate data. E.g. A product data might have been inserted earlier,and at a later time, orders table being inserted which links to product_id or something on product table. In this case, how can we fetch orders of a product from orders table with some extra information of the product from the product table using realm.io?
  • Realz
    Realz almost 9 years
    I think I'll just safely use DBFlow which is a wrapper for sqlite