Skip to content

Entity properties collision with generated properties for relations #713

@Fabricio-191

Description

@Fabricio-191

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.

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions