2

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.
}
DJ2695
  • 62
  • 2
  • 6
  • It's preferable to use `final ... = ToOne/ToMany` instead of `var` to ensure the object itself is never replaced (only the relationship target). – vaind Jul 19 '21 at 10:05
  • > as far as my testing went, all the builds I tried with multiple Many-To-Many Relation in one class failed. Could you be more specific? What were the errors? – vaind Jul 19 '21 at 10:05
  • @vaind I adjusted the examples and specified my question, that the issue lies within mixing `@Backlink` and no `@Backlink` and added the BuildError I receive while trying to build this – DJ2695 Jul 19 '21 at 11:59

1 Answers1

2
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
vaind
  • 1,642
  • 10
  • 19
  • thats good news. Would this be the correct way to override it ? : `dev_dependencies: objectbox_generator: git: git@github.com:objectbox/objectbox-dart.git dependency_overrides: objectbox_generator: version: '>=1.1.1' ` – DJ2695 Jul 19 '21 at 12:53
  • I've updated the answer to add the correct format – vaind Jul 19 '21 at 15:35
  • the fix makes the generator work like a charm, thank you. – DJ2695 Jul 20 '21 at 07:46