Describe the bug
When using lb4 relation to generate a belongsTo relation with a custom keyTo (targeting a non-primary-key field on the destination model), the CLI generator does not include keyTo in the generated @belongsTo decorator. Additionally, the foreign key type is always resolved from the destination model's primary key type instead of the actual keyTo field type.
This means the generated code is silently broken the relation resolves against the wrong field at runtime.
Note: The keyTo option is fully supported in @loopback/repository at runtime. The bug is only in the CLI generator not passing it through to the generated code.
Steps to reproduce:
-
Have two models where the target model has a non-PK unique field:
- Source model:
KundeThrough
- Target model:
Kunde with a unique field bexioId: string (not the PK)
-
Run: lb4 relation
- Relation type: belongsTo
- Source model: KundeThrough
- Target model: Kunde
- Foreign key name: contactId
- Custom keyTo: bexioId
-
Inspect the generated property in kunde-through.model.ts
Expected behavior:
@belongsTo(() => Kunde, {name: 'contact', keyTo: 'bexioId'})
contactId: string; // type resolved from bexioId (string), not PK (number)
Actual behavior:
@belongsTo(() => Kunde, {name: 'contact'})
contactId: number; // keyTo missing, FK type wrong (uses PK type instead)
Logs
Additional information
No response
Reproduction
n/a
Describe the bug
When using
lb4 relationto generate abelongsTorelation with a customkeyTo(targeting a non-primary-key field on the destination model), the CLI generator does not includekeyToin the generated@belongsTodecorator. Additionally, the foreign key type is always resolved from the destination model's primary key type instead of the actualkeyTofield type.This means the generated code is silently broken the relation resolves against the wrong field at runtime.
Note: The
keyTooption is fully supported in@loopback/repositoryat runtime. The bug is only in the CLI generator not passing it through to the generated code.Steps to reproduce:
Have two models where the target model has a non-PK unique field:
KundeThroughKundewith a unique fieldbexioId: string(not the PK)Run: lb4 relation
Inspect the generated property in
kunde-through.model.tsExpected behavior:
@belongsTo(() => Kunde, {name: 'contact', keyTo: 'bexioId'})
contactId: string; // type resolved from bexioId (string), not PK (number)
Actual behavior:
@belongsTo(() => Kunde, {name: 'contact'})
contactId: number; // keyTo missing, FK type wrong (uses PK type instead)
Logs
Additional information
No response
Reproduction
n/a