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

Not nullable object relationship #6195

Open
petmongrels opened this issue Oct 13, 2023 · 5 comments
Open

Not nullable object relationship #6195

petmongrels opened this issue Oct 13, 2023 · 5 comments

Comments

@petmongrels
Copy link

How frequently does the bug occur?

Always

Description

I have two objects comment and individual. Comment is linked to individual. We want to make the relationship from comment to individual not nullable but it is not taking effect.

Please see the code here.
https://github.com/avniproject/avni-client/blob/85ee69f3b9da94ba4dce5d0d249f878f6ccbac3a/packages/openchs-android/integrationTest/RealmIssuesApp.js

versions

"react-native": "0.72.3",
"realm": "11.8.0",

Stacktrace & log output

NA

Can you reproduce the bug?

Always

Reproduction Steps

I have the above component as a part of RN app.

Version

11.8.0

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 11 API 30 Genymotion

Build environment

Which debugger for React Native: ..
NA

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Oct 23, 2023

@petmongrels Links will always be nullable. In version 12.0.0 we have tied up the schema validation so it will mislead you. Prior to version 12.0.0, our schema validation wasn't strict.

If line 9 in your schema was interpreted strictly, line 33 would fail as subject is missing.

In Realm, a link is a many-to-one relationship. It can be that embedded objects is a better match for your data model.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Oct 23, 2023
@petmongrels
Copy link
Author

If line 9 in your schema was interpreted strictly, line 33 would fail as subject is missing.

Yes we want it to fail but it doesn't.
Also, in our use case is of entity to entity so many-to-one usually and we embedded (value object pattern) object is not a right fit.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Oct 24, 2023
@kneth
Copy link
Contributor

kneth commented Oct 24, 2023

Unfortunately it is not possible to specify that a link must be required (strictly speaking, Realm supports N:0 relationships, and not N:1 relationships). If it was possible, you could easily end in the situation where deleting an object was not possible. For example consider the following case:

const commentA = {
  uuid: "fooA",
};
const commentB = {
  uuid: "fooB",
};
const individual = {
  uuid: "bar",
  name: "footer",
};

realm.write(() => {
 const individual = realm.create("Individual", { uuid: "bar", name: "foobar", });
 realm.create("Comment", { uuid: "A", subject: individual });
 realm.create("Comment", { uuid: "B", subject: individual });
});

realm.write(() => {
  realm.delete(realm.objectByPrimaryKey("Individual", "foo"));
  // both Comment objects have property subject set to null --> if required, we cannot allow delete() on the Individual object
}); 

I believe it is similar to a foreign key in SQL.

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Needs-Attention Reporter has responded. Review comment. labels Oct 24, 2023
@petmongrels
Copy link
Author

Thank for your reply.

Understood that the support is not there. But the requirement is quite common. Individual/Comment is not the best example because both can be understood to be transactional data.

We could have Individuals with relationship lets says a Country. There are multiple individuals in a country or province (they can have their own lifecycle like change in name of province etc). Business-wise you never delete country in your application, only Individuals.

Hibernate/Spring-JPA support this for very long time and realm should also consider providing support for it.
Hibernate etc provide support for this by allowing user to define cascade property in the many to one relationship - https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/annotations/CascadeType.html

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Oct 24, 2023
@kneth
Copy link
Contributor

kneth commented Oct 25, 2023

@petmongrels Thank for the detailed suggestion. I will put the issue on our backlog and discuss "hot topics in data modelling" with a broader audience (aka the other SDK teams).

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants