Skip to content

Commit 433712e

Browse files
Generator: always find ToOne backlink source by ToOne field name #130
With recent changes the model has the name of the ToOne field stored in relationField, so just always use it to look up the target ID (or relation) property.
1 parent 42caccc commit 433712e

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

generator/lib/src/code_builder.dart

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@ class CodeBuilder extends Builder {
413413
);
414414
}
415415

416+
// Note that in the model only the target ID (or relation) property of a
417+
// ToOne exists, so search for that.
416418
if (bl.srcField.isEmpty) {
419+
// No source field name given, try to find the source relation by type of
420+
// its target entity.
417421
final matchingProps = srcEntity.properties.where(
418422
(p) => p.isRelation && p.relationTarget == entity.name,
419423
);
@@ -429,17 +433,12 @@ class CodeBuilder extends Builder {
429433
srcRel = matchingRels.first;
430434
}
431435
} else {
432-
// For backwards compatibility, expect the name of the ToOne field, so add
433-
// an "Id" suffix to find the target ID property.
436+
// Source field name given
437+
// For ToOne, expect the name of the ToOne field
434438
srcProp = srcEntity.properties.firstWhereOrNull(
435-
(p) => p.isRelation && p.name == '${bl.srcField}Id',
439+
(p) => p.isRelation && p.relationField == bl.srcField,
436440
);
437-
// Otherwise, and to support ToOne with renamed target ID properties
438-
// (using @TargetIdProperty), expect the name of the target ID property.
439-
srcProp ??= srcEntity.properties.firstWhereOrNull(
440-
(p) => p.isRelation && p.name == bl.srcField,
441-
);
442-
// For ToMany, always expect the name of the ToMany field/relation
441+
// For ToMany, expect the name of the ToMany field
443442
srcRel = srcEntity.relations.firstWhereOrNull(
444443
(r) => r.name == bl.srcField,
445444
);
@@ -458,10 +457,7 @@ class CodeBuilder extends Builder {
458457
} else {
459458
throw InvalidGenerationSourceError(
460459
'Failed to find backlink source for "${entity.name}.${bl.name}" in "${srcEntity.name}", '
461-
'make sure a matching ToOne or ToMany relation exists.'
462-
' If the ToOne target ID property is renamed with @TargetIdProperty,'
463-
' then make sure to specify the name of the target ID property instead of the ToOne,'
464-
" like @Backlink('<property>').",
460+
'make sure a matching ToOne or ToMany relation exists.',
465461
);
466462
}
467463
}

generator/test/code_builder_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,7 @@ void main() {
501501
@Id()
502502
int id = 0;
503503
504-
// Must specify the target ID property instead of the ToOne field
505-
@Backlink('customerRef')
504+
@Backlink('customer')
506505
final backRel = ToMany<Example>();
507506
}
508507
''';

0 commit comments

Comments
 (0)