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

℡╲_俬逩灬. 提交于 2021-01-27 04:45:30

问题


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()

回答1:


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



来源:https://stackoverflow.com/questions/31628574/implementing-a-form-of-join-query-on-a-realm-db-without-having-a-predefined-rela

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!