I have a many to many relations between two models. When I create 2 level cycle then duplicate objects are added to array. For example suppose I have two models User and Intersts and many to many relation between then like this:
User.schema = {
name: 'User',
primaryKey: 'id',
properties: {
id: { type: 'string', default: '' },
name: { type: 'string', default: '' },
interests: { type: 'list', objectType: 'Interest', default: [] },
}
}
Interest.schema = {
name: 'Interest',
primaryKey: 'id',
properties: {
id: { type: 'string', default: '' },
name: { type: 'string', default: '' },
interestedUsers: { type: 'list', objectType: 'User', default: [] },
}
}
When I try to insert following data with realm.create(<modelName>, <object>, true) it does that correctly: http://pastebin.com/pE1FFd5x
But on adding following data multiple entries for same interest is created for user: http://pastebin.com/FFLbR5CR
A demo project showing the issue is here: https://github.com/shahankit/realm-duplicate
Thanks for the detailed but report. In this case what is happening underneath in hood in the create codepath is that the same array property is being set multiple times at the same time. This ends up resulting in undefined behavior which will change based on the ordering of the data.
This is not ideal but I'm not sure it's worth adding cycle detection for this scenario which is how we would prevent this behavior. As a short term workaround you can leave the array property as undefined on the deeper User entry - this will result in the array property only being set a single time without duplicate entries. In the longer term we will be adding support for backlinks/virtual/many-to-may properties - once we add this interestedUsers will be generated automatically based on a Users interests and this should be a non-issue.
@alazier Thanks for checking on issue. I'll try the short term fix as for now.
I will close this as virtual properties/backlink access/bidirectional relationships are covered by issue #343
@alazier any timeline on when this will be solved?
We now have underlying core and object store support for computed properties and I hope this will make it into one of the next few releases, assuming we don't run into technical issues in how we expose this functionality in the js layer.
@alazier any updates on this issue. Because we now have more and more models with cyclic structures. Can you reopen the issue?
Will hopefully be able to start working on adding virtual properties/backlinks/#343 in the next two weeks.
Most helpful comment
Will hopefully be able to start working on adding virtual properties/backlinks/#343 in the next two weeks.