Flutter Objectbox:在@Backlink 和没有@Backlink 之间有多个 M:N (ManyToMany) 关系和混合是否有效

问题描述

ObjectBox 是否支持多个多对多关系 (M:N)? 例如:

// MIX with @Backlink and no @Backlink --> causes Error in this class
@Entity()
class Exercise {
int id;
String title;
String description;

final unitTypes = ToMany<UnitType>();

// Many To Many with Equipment
@Backlink()
final equipments = ToMany<Equipment>();

... Constructor etc.
}

// NO mix with @Backlink and no @Backlink --> no Error in this class
@Entity()
class UnitType{
 int id;
 String title;
 
 @Backlink()
 final exercises = ToMany<Exercises>();

 @Backlink()
 final units = ToMany<Unit>();

 ... Constructor etc.
}

@Entity()
class Unit{
 int id;
 String title;
 String short;

 final unitType = ToOne<UnitType>();

 ... Constructor etc.
}

就我的测试而言,我在一个类中使用多个多对多关系尝试的所有构建都失败了。抛出类似这样的错误

Could not format because the source Could not be parsed:

line 1078,column 212 of .: Expected to find '}'.
     ╷
1078 │         toManyRelations: (Exercise object) => {RelInfo<Exercise>.toMany(27,object.id): object.unitTypesRelInfo<ExerciseEquipment>.toOneBacklink(3,object.id,(ExerciseEquipment srcObject) => srcObject.exercise): object.exerciseEquipments,

那么有没有办法在 ObjectBox 中完成这项工作,或者我是否必须使用另一个具有一对多关系的 Binder 类,例如在运动和 UnitType 之间 --> ExerciseUnitType (此解决方法我有用,但并不美观,并且显着增加了对额外类和额外存储数据的需求

@Entity()
class ExerciseUnitType{
 int id;
 String title;
 
 @Backlink()
 final exercise= ToOne<Exercise>(); 

 @Backlink()
 final unitType = ToOne<UnitType>();

 ... Constructor etc.
}

解决方法

could not format because the source could not be parsed:

line 1078,column 212 of .: Expected to find '}'.
     ╷
1078 │         toManyRelations: (Exercise object) => {RelInfo<Exercise>.toMany(27,object.id): object.unitTypesRelInfo<ExerciseEquipment>.toOneBacklink(3,object.id,(ExerciseEquipment srcObject) => srcObject.exercise): object.exerciseEquipments,

是 objectbox-generator 代码中的一个错误 - 它为您的配置创建了一个语法错误的代码(缺少逗号)。显然,在同一实体中同时拥有链接和反向链接并不是集成测试的一部分。我已经解决了这个问题,你可以更新你的 pubspec.yaml 以临时使用来自 github 的最新 objectbox-generator(使用 dependency_override),或者等到修复在下一个 ObjectBox 版本中推出。要使用 Git 的固定版本:只需在 pubspec.yaml 的末尾添加以下代码

dependency_overrides:
  objectbox_generator:
    git:
      url: https://github.com/objectbox/objectbox-dart.git
      ref: 461a948439dcc42f3956b7d21b232eb9c2bc26e1
      path: generator