Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define relationship b/w two collections #4520

Closed
abhishekmatta999 opened this issue Mar 15, 2021 · 13 comments
Closed

Define relationship b/w two collections #4520

abhishekmatta999 opened this issue Mar 15, 2021 · 13 comments

Comments

@abhishekmatta999
Copy link

abhishekmatta999 commented Mar 15, 2021

I want to query and get the article result and in article schema we have defined a key 'article_meta' which is and objectId. Now I want article result with all the properties with article_meta as an object fetched on the basis of (article.article_meta = articlemeta._id). But when I'm trying to open a realm, sync starts but there is no data in the local db but when I tried it without the defining any relationship it works.

`const articleMetaSchema = {
name: 'articlemeta',
properties: {
_id: 'objectId?',
catalog_id: 'objectId?',
content: 'objectId?',
createdAt: 'date?',
description: 'string?',
main_image_url: 'string?',
origin_type: 'string?',
pub_date: 'date?',
publication: 'objectId?',
sub_title: 'string?',
title: 'string?',
updatedAt: 'date?',
url: 'string?'
},
primaryKey: '_id'
}

const articleSchema = {
name: 'article',
properties: {
_id: 'objectId?',
active_search: 'bool?',
article_meta: 'articlemeta?',
catalog_id: 'objectId?',
content: 'objectId?',
createdAt: 'date?',
flagged: 'bool?',
owner_id: 'objectId?',
rating: 'int?',
read: 'bool?',
status: 'string?',
status_updated_at: 'date?',
updatedAt: 'date?'
},
primaryKey: '_id'
}

config = {
schema,
path: getDBPath(),
sync: {
user: app.currentUser,
partitionValue: '--value--',
error: (error) => {
console.log(error.name, error.message)
}
}
}
let realm = await Realm.open(config)
// query
`

@ironage
Copy link
Contributor

ironage commented Mar 15, 2021

You don't have to specify the primary key _id of the relationship query. It should work to query for article.article_meta = articlemeta. I'm making some assumptions here though. If this doesn't help, can you provide the queries that you tried, as well as the schema for the class that your are filtering on? (the one with the link to article)

@abhishekmatta999
Copy link
Author

I want to query But when I'm defining the above relationship and trying to open a realm instance I'm not receiving any data to my local db.
Query is
let realmArticles = realm.objects('article').sorted('createdAt', true).slice((body.skip || 0), (body.limit || 30))

@ironage
Copy link
Contributor

ironage commented Mar 15, 2021

ok, is there any error coming back in the sync logs?

@ironage
Copy link
Contributor

ironage commented Mar 15, 2021

I'm not sure if this is the issue, but you should try this change in your article schema article_meta: 'articlemeta', as a relationship link is implicitly optional so you shouldn't need the ? there.

@abhishekmatta999
Copy link
Author

ok, is there any error coming back in the sync logs?

Here's the log:
image

@abhishekmatta999
Copy link
Author

I think this is the error: "Failed to transform received changeset: Schema mismatch: Property 'article_meta' in class 'article' is of type Link on one side and type ObjectId on the other."
Any solutions to this?

@ironage
Copy link
Contributor

ironage commented Mar 15, 2021

It sounds like the relationship on the server side hasn't picked up the relationship properly and is interpreting the property as a simple object id. You should be able to turn on development mode on the server and change it, see https://docs.mongodb.com/realm/sync/configure-your-data-model/#view-and-fix-schema-errors
You may have to delete the local development app data as well.

@abhishekmatta999
Copy link
Author

Is it possible to make relationship b/w two collections which have different partition keys.If Yes how?

What should be the approach if I want to sync two collections with different partition keys.

  1. _partition_key : 'catalog_id=someid'
  2. _partition_key : 'PUBLIC'
    Should I open two different instance of realm or how?

@nirinchev
Copy link
Member

Each partition value will end up in a different Realm instance. Any relationships that you set up between the collections that fall into different Realm instances will be null locally. If your app needs to keep references between different partitions, then you should not setup relationships and instead use the Ids of the objects and look them up manually. Be aware though that Realm will not impose any referential guarantees there, so it'll always be possible to end up in a situation where an Object from Realm 1 points to an id that is supposed to exist in Realm 2, but doesn't.

You can read up on how partitioning works in the docs: https://docs.mongodb.com/realm/sync/partitioning/.

@abhishekmatta999
Copy link
Author

How Can I query with "IN" operator. I wanted to get filtered results on basis of some ObjectIds. How Can I achieve this?

Query so far:
let publications = publicRealm.objects('catalogpublication').filtered(_id IN ${publicationIds}`)

@bmunkholm
Copy link
Contributor

@abhishekmatta999 As we strive to use github for bugs and feature requests only I would advise posting usage questions in our Forums for MongoDB Realm about sync in general and the SDK forums for specific SDK questions.
Thanks!

@ironage
Copy link
Contributor

ironage commented Mar 18, 2021

@abhishekmatta999 this is not yet supported (tracked here) but you can use a workaround by writing out the long form as described here.

@fealebenpae
Copy link
Member

fealebenpae commented Mar 22, 2021

Hey @abhishekmatta999, as @bmunkholm said above the Forums are the best place to get general help. This repository is only monitored by the engineers working on the Mobile Database. You're welcome to submit questions and bug reports here about the embedded database specifically.
I'm going to close this issue - please check out our Forums for help and support if we couldn't answer your questions here.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants