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

Inverse Relationship not working as expected #6182

Closed
saravanakumargn opened this issue Oct 6, 2023 · 2 comments
Closed

Inverse Relationship not working as expected #6182

saravanakumargn opened this issue Oct 6, 2023 · 2 comments

Comments

@saravanakumargn
Copy link

How frequently does the bug occur?

Always

Description

Can you please check this? Any issue with my schema or it is bug?

import Realm from "realm";

const TeacherSchema = {
  name: "Teacher",
  primaryKey: "teacherId",
  properties: {
    teacherId: { type: "int" },
    teacherName: { type: "string" },
    students: { type: "list", objectType: "Student" },
  },
};

const StudentSchema = {
  name: "Student",
  primaryKey: "studentId",
  properties: {
    studentId: { type: "int" },
    name: { type: "string" },
    teacher: {
      type: "linkingObjects",
      objectType: "Teacher",
      property: "students",
    },
  },
};

const realm = new Realm({
  path: "./student_realm/student.realm",
  schema: [TeacherSchema, StudentSchema],
});

const teacherData = {
  teacherId: 1,
  teacherName: "Maths Teacherr",
};

realm.write(() => {
  realm.create("Teacher", teacherData, Realm.UpdateMode.Modified);
});

realm.write(() => {
  const teacherResult = realm.objectForPrimaryKey("Teacher", 1);
  const studentData = {
    studentId: 1,
    name: "student name1",
    teacher: teacherResult,
  };

  realm.create("Student", studentData, Realm.UpdateMode.Modified);
});
console.log('done')

realm.close();

Screenshot 2023-10-06 at 6 14 31 PM
Screenshot 2023-10-06 at 6 14 46 PM

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

"realm": "^12.2.1",

What services are you using?

-- select --

Are you using encryption?

No

Platform OS and version(s)

Mac, NodeJS

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Oct 23, 2023

@saravanakumargn linkingObjects is a computed property, and you cannot use it to create the link between the object. Your second transaction should be:

realm.write(() => {
  const teacherResult = realm.objectForPrimaryKey("Teacher", 1);
  const studentData = {
    studentId: 1,
    name: "student name1",
  };
  const student = realm.create("Student", studentData, Realm.UpdateMode.Modified);
  teacherResult.students.push(student);
});

@saravanakumargn
Copy link
Author

I missed this. Thanks @kneth.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 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

2 participants