Flutter Objectbox: Does it work to have multiple M:N (ManyToMany) Relations and mix between @Backlink and no @Backlink

Issue

Does Objectbox Support having multiple ManyToMany Relations (M:N) ?
For example:

// 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.
}

as far as my testing went, all the builds I tried with multiple Many-To-Many Relation in one class failed. Throwing similar errors like this:

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,

So is there a way to in ObjectBox do make this work, or do I have to use another Binder Class with a One-To-Many Relationship, for example between Exercise and UnitType –> ExerciseUnitType
(this workaround has worked for me, but is not pretty and increases the need of additional classes and additional store data significantly)

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

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

 ... Constructor etc.
}

Solution

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,

is an error in the objectbox-generator code – it created a syntactically incorrect code for your configuration (a comma was missing). Apparently having both links and backlinks in the same entity wasn’t part of the integration tests. I’ve fixed that and you can update your pubspec.yaml to use the latest objectbox-generator from github temporarily (using a dependency_override), or wait until the fix rolls out in the next ObjectBox release. To use the fixed version from Git: just add the following code at the end of your pubspec.yaml

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

Answered By – vaind

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.