Realm-js: Queries across backlinks (linking objects)

Created on 16 Nov 2017  路  6Comments  路  Source: realm/realm-js

Goals

Create query across backlink just like you would do with regular links.

Expected Results

Successfully retrieve results.

Actual Results

Property 'onSale' is not a link in object of type 'Combination'

Steps to Reproduce

class Product {};
  Product.schema = {
    name: 'Product',
    primaryKey:'productId',
    properties: {
        productId:'int',
        product:'string'
        combinations: {type: 'linkingObjects', objectType: 'Combination', property: 'products'}
    }
};

class Combination {};
Combination.schema = {
    name: 'Combination',
    properties: {
        onSale: {type: 'string'},
        products: {type: 'list',objectType:'Product'},
    }
};

and

let queryFilter = 'combinations.onSale = "yes" AND productId = 1';
let data = realm.objects('Product').filtered(queryFilter);

Version of Realm and Tooling

  • Realm JS SDK Version: the latest at this time
  • Node or React Native: .
  • Client OS & Version: .
  • Which debugger for React Native: None
T-Bug

Most helpful comment

Closing as #1660 has been merged. It will be released soon.

All 6 comments

I have reproduced it in pure Javascript:

const Realm = require('realm')

const ProductSchema = {
    name: 'Product',
    primaryKey:'productId',
    properties: {
        productId:'int',
        product:'string',
        combinations: {type: 'linkingObjects', objectType: 'Combination', property: 'products'}
    }
}

const CombinationSchema = {
    name: 'Combination',
    properties: {
        onSale: {type: 'string'},
        products: {type: 'list', objectType:'Product'}
    }
}

Realm.open({
    schema: [ProductSchema, CombinationSchema]
}).then(function (realm) {
    let queryFilter1 = 'onSale = "yes" AND products.productId = 1'
    let data1 = realm.objects('Combination').filtered(queryFilter1)

    let queryFilter2 = 'combinations.onSale = "yes" AND productId = 1'
    let data2 = realm.objects('Product').filtered(queryFilter2)
})

Closing as #1660 has been merged. It will be released soon.

Amazing work :smile:

All credit go to @ironage

Has this been released? I'm using 2.6.0 and still cannot get queries across backlinks to work. Two documentation sources claim that it is supported:

From https://realm.io/docs/javascript/latest#queries:

  • A key path can also follow linking objects (backlinks), e.g. parents.age > 25 and parents.@count == 2.

From: https://github.com/realm/realm-js/blob/master/docs/tutorials/query-language.md:

Other objects can link to an object and you can query on that releationship using the @links and @links.ClassName.PropertyName syntax. If the relationship of the LinkingObject is named, use the name in the query just like you would use any other property for readability. If the relationship is not named, you can use the @links.ClassName.PropertyName syntax where ClassName.PropertyName describes the forward relationship.

I cannot get either option to work.

@sellmeadog yes, I'd expect it to be working since 2.3.4. Are you able to share your query and the schemas of the linked classes if not your actual models, a minimal example that can reproduce the behaviour you see?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bdebout picture bdebout  路  3Comments

CrystalRanita picture CrystalRanita  路  3Comments

MihaelIsaev picture MihaelIsaev  路  3Comments

jmartindivmedianet picture jmartindivmedianet  路  3Comments

max-zu picture max-zu  路  3Comments