为什么在 flutter 中使用“ObjectBox”将相同的子项添加到第二个父项时,第一个父项模型引用被设置为 null?

发布于 2025-01-16 02:51:09 字数 644 浏览 1 评论 0原文

我有两个对象,一个父模型具有 ToOne 关系,另一个子模型与父模型具有 ToMany 关系。现在,如果我有两个父母正在设置与孩子的关系,那么当设置第二个父母引用时,第一个父母对孩子的引用将变为空。另外,如果我从孩子中删除 id 字段,那么父母双方的孩子都会存储在数据库中。

假设我现在有一个像这样的班级

Student{
  String id;
  final teacher = ToOne<Teacher>();
}

和另一个老师这样的班级,

Teacher{
  @Unique(onConflict: ConflictStrategy.replace)
  String? id;
  @Backlink()
  final students = ToMany<Student>();
}

如果我尝试执行类似的操作

student1.teacher.target = teacher1

student2.teacher.target = teacher1

那么如果我尝试检查学生1,那么它的老师将为空。为什么 ?

I have two objects one parent model have ToOne relation and a child model with ToMany relation back to the parent. Now if I have two parents who are setting their relation to child then the first parent's reference to child is getting null when the second parent reference is set. Also, if I remove the id field from the child then both parents child is stored in the database.

Suppose I have a class like

Student{
  String id;
  final teacher = ToOne<Teacher>();
}

and another class Teacher as

Teacher{
  @Unique(onConflict: ConflictStrategy.replace)
  String? id;
  @Backlink()
  final students = ToMany<Student>();
}

so now if I try to do something like

student1.teacher.target = teacher1

followed by

student2.teacher.target = teacher1

then if I try to check for student1 then its teacher is getting null. Why ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文