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

333
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
Share:
333
DJ2695
Author by

DJ2695

Updated on November 25, 2022

Comments

  • DJ2695
    DJ2695 over 1 year

    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.
    }
    
    • vaind
      vaind almost 3 years
      It's preferable to use final ... = ToOne/ToMany instead of var to ensure the object itself is never replaced (only the relationship target).
    • vaind
      vaind almost 3 years
      > 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?
    • DJ2695
      DJ2695 almost 3 years
      @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
    DJ2695 almost 3 years
    thats good news. Would this be the correct way to override it ? : dev_dependencies: objectbox_generator: git: [email protected]:objectbox/objectbox-dart.git dependency_overrides: objectbox_generator: version: '>=1.1.1'
  • vaind
    vaind almost 3 years
    I've updated the answer to add the correct format
  • DJ2695
    DJ2695 almost 3 years
    the fix makes the generator work like a charm, thank you.