Skip to content

Detach method id reference is being generated all lowercase instead of lower camel case #3391

Closed

Description

@marcelomendoncasoares

Describe the bug

When generating the detach method for related classes, the id parameter is being converted to all lower case instead of lower camel case. For example, with a reference class called CitizenInt, the detach method is being generated as:

class CitizenIntDetachRowRepository {
  const CitizenIntDetachRowRepository._();

  /// Detaches the relation between this [CitizenInt] and the [AddressUuid] set in `address`
  /// by setting the [CitizenInt]'s foreign key `id` to `null`.
  ///
  /// This removes the association between the two models without deleting
  /// the related record.
  Future<void> address(
    _i1.Session session,
    CitizenInt citizenint, {  // <-- Should be `citizenInt`, with capital "I" for the second word.
    _i1.Transaction? transaction,
  }) async {

To reproduce

Create two models with more than one word in the name (i.e.: AddressUuid and CitizenInt) and with object relations to each other:

class: AddressUuid
table: address_uuid
fields:
  street: String
  inhabitantId: int?
  inhabitant: CitizenInt?, relation(name=citizen_address, field=inhabitantId)
indexes:
  inhabitant_uuid_index_idx:
    fields: inhabitantId
    unique: true
class: CitizenInt
table: citizen_int
fields:
  name: String
  address: AddressUuid?, relation(name=citizen_address)

Now check the generated CitizenIntRepository to see that the address method of the CitizenIntDetachRowRepository has the parameter citizenint (all lower case), while the address method of the CitizenIntAttachRowRepository has the parameter correctly generated as citizenInt.

class CitizenIntAttachRowRepository {
  const CitizenIntAttachRowRepository._();

  /// Creates a relation between the given [CitizenInt] and [AddressUuid]
  /// by setting the [CitizenInt]'s foreign key `id` to refer to the [AddressUuid].
  Future<void> address(
    _i1.Session session,
    CitizenInt citizenInt,  // Correct
    _i2.AddressUuid address, {
    _i1.Transaction? transaction,
  }) async {
class CitizenIntDetachRowRepository {
  const CitizenIntDetachRowRepository._();

  /// Detaches the relation between this [CitizenInt] and the [AddressUuid] set in `address`
  /// by setting the [CitizenInt]'s foreign key `id` to `null`.
  ///
  /// This removes the association between the two models without deleting
  /// the related record.
  Future<void> address(
    _i1.Session session,
    CitizenInt citizenint, {  // Incorrect
    _i1.Transaction? transaction,
  }) async {

Expected behavior

The CitizenInt parameter should preserve the case of the class name, with only the first letter as lower case - such as already happens for the attach repository.

Library version

2.4.0

Platform information

Windows

Additional context

The issue is probably caused by the conversion to lower camel case in the _buildDetachRowFromObjectRelationField method of the repository_classes.dart file:

  Method _buildDetachRowFromObjectRelationField(String className,
      SerializableModelFieldDefinition field, ClassDefinition classDefinition) {
    return Method((methodBuilder) {
      var classFieldName = className.toCamelCase(isLowerCamelCase: true);  // this should be `.camelCase`
      var fieldName = field.name;

      var relation = field.relation as ObjectRelationDefinition;

      var idTypeReference = relation.parentTableIdType.reference(
        serverCode,
        nullable: false,
        subDirParts: classDefinition.subDirParts,
        config: config,
      ) as TypeReference;

The toCamelCase function is prepared to convert from words separated by spaces or underscores, but not terms that are already as some camel case form. All other parts of the repository_classes.dart use the .camelCase method of the recase package.

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    on Apr 7, 2025
  2. added
    area: models
    breaking
    on Apr 7, 2025
  3. SandPod commented on Apr 7, 2025

    @SandPod
    Contributor

    Thanks for reporting this! 👍

    Since this will change the generated interface we will need to fix this in the next major version.

  4. removed
    breaking
    on Oct 24, 2025
  5. removed this from the 3.0 Release milestone on Oct 24, 2025
  6. moved this from Backlog 🗂️ to In Progress 🔥 in Serverpod Frameworkson Oct 24, 2025
  7. SandPod commented on Oct 24, 2025

    @SandPod
    Contributor

    This is actually not a breaking change since it modified a positional parameter.

  8. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: modelsbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions