Flutter Objectbox:在@Backlink 和没有@Backlink 之间有多个 M:N (ManyToMany) 关系和混合是否有效
Flutter Objectbox: Does it work to have multiple M:N (ManyToMany) Relations and mix between @Backlink and no @Backlink
对象框是否支持多个多对多关系 (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.
}
就我的测试而言,我尝试在一个 class 中使用多个多对多关系的所有构建都失败了。抛出类似这样的错误:
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 Class,例如在 Exercise 和 UnitType 之间 --> ExerciseUnitType
(这个变通方法对我有用,但不是很好并且显着增加了对额外 classes 和额外商店数据的需求)
@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,
是对象框生成器代码中的一个错误 - 它为您的配置创建了语法错误的代码(缺少逗号)。显然,在同一实体中同时拥有链接和反向链接并不是集成测试的一部分。我已经解决了这个问题,您可以更新 pubspec.yaml 以暂时使用 github 中的最新对象框生成器(使用 dependency_override
),或者等到修复程序在下一个 ObjectBox 中推出发布。要使用 Git 中的固定版本:只需在 pubspec.yaml
的末尾添加以下代码
dependency_overrides:
objectbox_generator:
git:
url: https://github.com/objectbox/objectbox-dart.git
ref: 461a948439dcc42f3956b7d21b232eb9c2bc26e1
path: generator
对象框是否支持多个多对多关系 (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.
}
就我的测试而言,我尝试在一个 class 中使用多个多对多关系的所有构建都失败了。抛出类似这样的错误:
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 Class,例如在 Exercise 和 UnitType 之间 --> ExerciseUnitType (这个变通方法对我有用,但不是很好并且显着增加了对额外 classes 和额外商店数据的需求)
@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,
是对象框生成器代码中的一个错误 - 它为您的配置创建了语法错误的代码(缺少逗号)。显然,在同一实体中同时拥有链接和反向链接并不是集成测试的一部分。我已经解决了这个问题,您可以更新 pubspec.yaml 以暂时使用 github 中的最新对象框生成器(使用 dependency_override
),或者等到修复程序在下一个 ObjectBox 中推出发布。要使用 Git 中的固定版本:只需在 pubspec.yaml
dependency_overrides:
objectbox_generator:
git:
url: https://github.com/objectbox/objectbox-dart.git
ref: 461a948439dcc42f3956b7d21b232eb9c2bc26e1
path: generator