objectbox-dart
objectbox-dart copied to clipboard
Entity properties collision with generated properties for relations
Is there an existing issue?
- [x] I have searched existing issues
Use case
I'm trying to create an entity like this:
@Entity()
class ActivityRatingEntity {
ActivityRatingEntity({
required this.objectBoxID,
required this.userId,
required this.activityId,
required this.comment,
required this.rating,
required this.createdAt,
required this.updatedAt,
});
@Id()
int objectBoxID;
final String userId;
final String activityId;
final String? comment;
final String rating;
//
final DateTime createdAt;
final DateTime updatedAt;
//
final user = ToOne<UserEntity>();
final activity = ToOne<ActivityEntity>();
}
The problem is the properties userId and activityId collide during code generation with userId and activityId properties generated for the relationships user and activity
And you get the error Cannot use the default constructor of 'ActivityRatingEntity': don't know how to initialize param userId - no such property. which is misleading
Here is the generated structure without my userId and activityId properties
{
"id": "8:589442412455634677",
"name": "userId",
"type": 11,
"flags": 520,
"indexId": "74:4100717343160108475",
"relationTarget": "UserEntity"
},
{
"id": "9:169920252546535936",
"name": "activityId",
"type": 11,
"flags": 520,
"indexId": "75:374444700250172517",
"relationTarget": "ActivityEntity"
}
Here is the structure without the relations
{
"id": "8:589442412455634677",
"name": "userId",
"type": 9
},
{
"id": "9:169920252546535936",
"name": "activityId",
"type": 9
}
Proposed solution
Add documentation to avoid creating properties like <relation name>Id
Change the error message to something more useful (maybe first add a mechanism/condition to detect the collisions)
Describe alternatives you've considered
Renaming the relationships or attributes is the obvious solution
Still, should be documented and the error message should be more useful
Additional context
I speak Spanish, English is my second language, and i see no way that error message pointing me the right direction, maybe i understand it wrong.
Thanks for reporting! There is https://docs.objectbox.io/relations#how-toone-works-behind-the-scenes But I agree this can be improved for Dart, including the generator error message.
Note for us: internal issue objectbox-dart#140
@greenrobot-team +1 for this - slightly different use case, we are implementing ObjectBox Sync with MongoDb and the automatic naming of relations by the Dart generator is breaking sync, as the relationship field names now do not match the existing MongoDb field names
@pwaltron Thanks for your note. The next release will, similar to Java, introduce a @TargetIdProperty annotation. It will allow to set a name different from the default name for the target ID property created for a ToOne:
// Instead of the default "customerId", property is named "customerRef"
@TargetIdProperty('customerRef')
final customer = ToOne<Customer>();
Let us know if this won't resolve your issue.
In addition, the generator will also now error and provide solutions in case a "regular" property name conflicts with one implicitly created for a ToOne relation.
Release 5.0.1 is available which introduces the mentioned @TargetIdProperty annotation and also provides a helpful error message in case a regular property collides with an implicitly generated target ID property for a ToOne relation.
To update to this release, for a Flutter project run flutter pub upgrade or for a Dart Native project dart pub upgrade.