Skip to content

Entity properties collision with generated properties for relations #713

Open
@Fabricio-191

Description

@Fabricio-191

Is there an existing issue?

  • 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.

Activity

greenrobot-team

greenrobot-team commented on Mar 17, 2025

@greenrobot-team
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @greenrobot-team@Fabricio-191

        Issue actions

          Entity properties collision with generated properties for relations · Issue #713 · objectbox/objectbox-dart