File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -369,7 +369,10 @@ class CodeBuilder extends Builder {
369369 }
370370 rel.targetId = targetEntity.id;
371371 }
372-
372+ }
373+ // Note: finding the backlink source requires that all ToMany relations have
374+ // a targetId set, so find backlink sources in a separate loop.
375+ for (var entity in model.entities) {
373376 for (var backlink in entity.backlinks) {
374377 backlink.source = _findBacklinkSource (model, entity, backlink);
375378 }
Original file line number Diff line number Diff line change @@ -271,6 +271,59 @@ void main() {
271271 ''' );
272272 });
273273
274+ test ('Finds backlink source if type is unique' , () async {
275+ final source = r'''
276+ library example;
277+ import 'package:objectbox/objectbox.dart';
278+
279+ @Entity()
280+ class Example {
281+ @Id()
282+ int id = 0;
283+
284+ final relA = ToMany<A>();
285+ final relB = ToOne<B>();
286+ }
287+
288+ // Name related classes to be lexically before Example so they are
289+ // processed first.
290+
291+ @Entity()
292+ class A {
293+ @Id()
294+ int id = 0;
295+
296+ @Backlink()
297+ final backRel = ToMany<Example>();
298+ }
299+
300+ @Entity()
301+ class B {
302+ @Id()
303+ int id = 0;
304+
305+ @Backlink()
306+ final backRel = ToMany<Example>();
307+ }
308+ ''' ;
309+
310+ final testEnv = GeneratorTestEnv ();
311+ await testEnv.run (source);
312+
313+ final entityA = testEnv.model.entities.firstWhere ((e) => e.name == 'A' );
314+ var backlinkSourceA = entityA.backlinks.first.source;
315+ expect (backlinkSourceA, isA <BacklinkSourceRelation >());
316+ expect ((backlinkSourceA as BacklinkSourceRelation ).srcRel.name, 'relA' );
317+
318+ final entityB = testEnv.model.entities.firstWhere ((e) => e.name == 'B' );
319+ var backlinkSourceB = entityB.backlinks.first.source;
320+ expect (backlinkSourceB, isA <BacklinkSourceProperty >());
321+ expect (
322+ (backlinkSourceB as BacklinkSourceProperty ).srcProp.relationField,
323+ 'relB' ,
324+ );
325+ });
326+
274327 test ('@TargetIdProperty ToOne annotation' , () async {
275328 final source = r'''
276329 library example;
Original file line number Diff line number Diff line change 1515 MongoDB to match the name used in the MongoDB database. [ #713 ] ( https://github.com/objectbox/objectbox-dart/issues/713 )
1616* Provide a helpful error message if the name of a property conflicts with a target ID property
1717 created for a ` ToOne ` relation. [ #713 ] ( https://github.com/objectbox/objectbox-dart/issues/713 )
18+ * Generator: find ` @Backlink() ` source relation also in case target entity class is processed first.
19+ [ #687 ] ( https://github.com/objectbox/objectbox-dart/issues/687 )
1820
1921### Sync
2022
You can’t perform that action at this time.
0 commit comments