@@ -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 }
0 commit comments