loopbackio / loopbackio/loopback-next

Polymorphic Relation - Wrong Doc

Open
#8,667 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Docs
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

Describe the bug

Hi there!

Let's suppose a base Delivery model with 3 fields: id, deliverableType, and deliverableId.
If you follow the docs, you'll get this error when requesting some deliverable relations:

Request GET /deliveries?filter=whatever failed with status code 500. Error: ER_BAD_FIELD_ERROR: Unknown column 'deliverable' in 'field list'

This is caused by this part of code, as you're asking it to search on the corresponding datasource for the field "deliverable" which doesn't exist:

class Delivery extends Entity {
@hasOne(() => Deliverable, {polymorphic: true})
  deliverable: Deliverable;

deliverableType: string;
}

Instead, it should be like this (search deliverableId on deliverableType's model):

class Delivery extends Entity {
@hasOne(() => Deliverable, {polymorphic: true})
  deliverableId: string;

deliverableType: string;
id: string;
}

With that change, polymorphic relation is working as expected (assuming repositories are correctly configured)

Logs

No response

Additional information

No response

Reproduction

Not needed

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Polymorphic relation documentation at https://loopback.io/doc/en/lb4/Polymorphic-relation.html and compare its Delivery example with the issue's reported fields. Update the example so the relation uses deliverableId and deliverableType, then verify that the documented example no longer requests the nonexistent deliverable field.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.