Skip to content

fix(cli): support keyTo option in belongsTo relation generator#11648

Open
Abubakar-K-Back wants to merge 1 commit into
loopbackio:masterfrom
Abubakar-K-Back:fix/belongs-to-keyto-support
Open

fix(cli): support keyTo option in belongsTo relation generator#11648
Abubakar-K-Back wants to merge 1 commit into
loopbackio:masterfrom
Abubakar-K-Back:fix/belongs-to-keyto-support

Conversation

@Abubakar-K-Back

Copy link
Copy Markdown

Description section:
Fixes #11647

When generating a belongsTo relation with a custom keyTo field, the CLI generator was silently ignoring the keyTo option it was not included in the generated @belongsTo decorator, and the FK type was always resolved from the destination model's primary key type instead of the actual keyTo field type.

Changes:

  • Pass keyTo to getBelongsTo() and include it in the decorator arguments
  • Resolve fktype from the actual keyTo field type using getModelPropertyType()
  • Pass keyTo into artifactInfo in generateControllers() for controller template access
  • Update controller EJS template to reflect the resolved keyTo field in the OpenAPI description

@Abubakar-K-Back Abubakar-K-Back force-pushed the fix/belongs-to-keyto-support branch from e0f623e to 5fc5eea Compare July 6, 2026 15:31
@aaqilniz

aaqilniz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Hi, @Abubakar-K-Back. Can you please add the tests for your changes? And you need to fix the commit message.
loopback-next uses commitzen to generate commit messages.

@Abubakar-K-Back Abubakar-K-Back force-pushed the fix/belongs-to-keyto-support branch from 5fc5eea to cb196ee Compare July 10, 2026 16:22
@Abubakar-K-Back Abubakar-K-Back changed the title add support for custom keyTo property in belongsTo relation generator… fix(cli): support keyTo option in belongsTo relation generator Jul 10, 2026

@aaqilniz aaqilniz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good.

@aaqilniz

Copy link
Copy Markdown
Contributor

Hi, @Abubakar-K-Back. Can you please squash the commits into one commit?

Adds keyTo support to the belongsTo relation CLI generator:
- Passes keyTo to getBelongsTo() decorator so the FK can resolve
  to a non-PK field on the target model
- Resolves fktype from the target model's keyTo property type
- Reflects keyTo in the controller OpenAPI description
- Propagates keyTo from --config CLI option to artifactInfo in
  the relation generator scaffold step
- Adds integration tests and snapshots for keyTo with and without
  a custom relation name

Fixes loopbackio#11594

Signed-off-by: Abubakar Awan <abkawan6@gmail.com>
@Abubakar-K-Back Abubakar-K-Back force-pushed the fix/belongs-to-keyto-support branch from 7922bf3 to 47ec90a Compare July 13, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI generator ignores keyTo option when creating belongsTo relations

3 participants