Skip to content

Entity properties collision with generated properties for relations #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
Fabricio-191 opened this issue Mar 14, 2025 · 1 comment
Open
1 task done
Labels
enhancement New feature or request

Comments

@Fabricio-191
Copy link

Is there an existing issue?

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.

@Fabricio-191 Fabricio-191 added the enhancement New feature or request label Mar 14, 2025
@greenrobot-team
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants