Skip to content

feat: support non-primary reference keys of source and target models for hasManyThrough - #11436

Open
aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/custom-reference-keys
Open

aaqilniz wants to merge 1 commit into
loopbackio:masterfrom
aaqilniz:feat/custom-reference-keys

Conversation

@aaqilniz

@aaqilniz aaqilniz commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

Loopback offers the hasManyThrough relation with keysFrom & keysTo using primary keys only.
You can also define a hasManyThrough relation using non-primary but unique keys.
Like in the following example:

Customers
  id (PK)
  customerCode (UNIQUE)
  name

Product
  id (PK)
  sku (UNIQUE)
  name

Orders (through model)
  id (PK)
  customerCode (referencing customer)
  productSku     (referencing product)
  quantity

The product and customers are connected with Order not through primary keys but through unique keys.

This is how it looks:

// for product
@hasMany(() => Customer, {
    customReferenceKeyFrom: 'sku',
    customReferenceKeyTo: 'customerCode',
    through: {
      model: () => Order,
      keyFrom: 'productSku',
      keyTo: 'customerCode',
    },
  })
customers: Customer[];

Please note that this is backward compatible. There is no breaking change in the PR.

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 21796125793

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 54.821%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/repository/src/relations/has-many/has-many-through.helpers.ts 0 4 0.0%
Totals Coverage Status
Change from base Build 21725088232: -0.03%
Covered Lines: 9572
Relevant Lines: 12459

💛 - Coveralls

@aaqilniz
aaqilniz marked this pull request as ready for review September 19, 2026 16:11
@aaqilniz
aaqilniz force-pushed the feat/custom-reference-keys branch from 463cd85 to 52b4437 Compare September 19, 2026 16:14
@aaqilniz

Copy link
Copy Markdown
Contributor Author

Hi, @dhmlau. Can you please have a look at this PR?

@aaqilniz
aaqilniz marked this pull request as draft September 19, 2026 17:13
@aaqilniz
aaqilniz marked this pull request as ready for review September 19, 2026 17:23
…for hasManyThrough

Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
@aaqilniz
aaqilniz force-pushed the feat/custom-reference-keys branch from f3e515f to 42411b5 Compare September 22, 2026 11:09

This branch has not been deployed

No deployments
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.

2 participants