@@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/element.dart';
66import 'package:analyzer/dart/element/nullability_suffix.dart' ;
77import 'package:analyzer/dart/element/type.dart' ;
88import 'package:build/build.dart' ;
9+ import 'package:collection/collection.dart' ;
910import 'package:objectbox/internal.dart' ;
1011import 'package:objectbox/objectbox.dart' ;
1112import 'package:source_gen/source_gen.dart' ;
@@ -391,10 +392,11 @@ class EntityResolver extends Builder {
391392 }
392393 }
393394
395+ _checkNoPropertiesConflictWithRelationProperties (entity, classElement);
394396 // Verify there is at most 1 unique property with REPLACE strategy.
395- ensureSingleUniqueReplace (entity, classElement);
397+ _ensureSingleUniqueReplace (entity, classElement);
396398 // If sync enabled, verify all unique properties use REPLACE strategy.
397- ifSyncEnsureAllUniqueAreReplace (entity, classElement);
399+ _ifSyncEnsureAllUniqueAreReplace (entity, classElement);
398400
399401 for (var p in entity.properties) {
400402 log.info (' $p ' );
@@ -618,7 +620,34 @@ class EntityResolver extends Builder {
618620 }
619621 }
620622
621- void ensureSingleUniqueReplace (
623+ /// Verifies no regular properties are named like ToOne relation
624+ /// properties (which are implicitly created and not defined as a Dart field,
625+ /// so their existence isn't obvious).
626+ void _checkNoPropertiesConflictWithRelationProperties (
627+ ModelEntity entity,
628+ ClassElement classElement,
629+ ) {
630+ final relationProps = entity.properties.where ((p) => p.isRelation);
631+ final nonRelationProps = entity.properties.whereNot ((p) => p.isRelation);
632+ 633+ for (var relProp in relationProps) {
634+ var propWithSameName = nonRelationProps.firstWhereOrNull (
635+ (p) => p.name == relProp.name,
636+ );
637+ if (propWithSameName != null ) {
638+ final conflictingField = classElement.fields.firstWhereOrNull (
639+ (f) => f.displayName == propWithSameName.name,
640+ );
641+ throw InvalidGenerationSourceError (
642+ 'Property name conflicts with the relation property "${relProp .name }" created for the ToOne relation "${relProp .relationField }".'
643+ ' Rename the property or use @TargetIdProperty on the ToOne to rename the relation property.' ,
644+ element: conflictingField,
645+ );
646+ }
647+ }
648+ }
649+ 650+ void _ensureSingleUniqueReplace (
622651 ModelEntity entity,
623652 ClassElement classElement,
624653 ) {
@@ -633,7 +662,7 @@ class EntityResolver extends Builder {
633662 }
634663 }
635664
636- void ifSyncEnsureAllUniqueAreReplace (
665+ void _ifSyncEnsureAllUniqueAreReplace (
637666 ModelEntity entity,
638667 ClassElement classElement,
639668 ) {
0 commit comments