loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Polymorphic Relation - Wrong Doc

Open A-Corregidor opened this issue 3 years ago • 4 comments

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

A-Corregidor avatar Jun 08 '22 09:06 A-Corregidor

Thanks for raising this issue. I will check the example given in the doc. Is the repository specified properly with deliverable field?

btw a working example in the test: https://github.com/loopbackio/loopback-next/blob/1e48456fde20e691d85b506f5c8e296c3c64b1fe/packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts#L64 https://github.com/loopbackio/loopback-next/blob/1e48456fde20e691d85b506f5c8e296c3c64b1fe/packages/repository-tests/src/crud/relations/acceptance/has-one.relation.polymorphic.acceptance.ts#L197

OnTheThirdDay avatar Jun 26 '22 13:06 OnTheThirdDay

I have upload an example repo at https://github.com/OnTheThirdDay/lb4-polymorphic-example

OnTheThirdDay avatar Jun 27 '22 04:06 OnTheThirdDay

Thanks for raising this issue. I will check the example given in the doc. Is the repository specified properly with deliverable field?

btw a working example in the test:

https://github.com/loopbackio/loopback-next/blob/1e48456fde20e691d85b506f5c8e296c3c64b1fe/packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts#L64

https://github.com/loopbackio/loopback-next/blob/1e48456fde20e691d85b506f5c8e296c3c64b1fe/packages/repository-tests/src/crud/relations/acceptance/has-one.relation.polymorphic.acceptance.ts#L197

Hi @OnTheThirdDay ! Repository documentation is OK

A-Corregidor avatar Jul 04 '22 06:07 A-Corregidor

Hi. There's a bug which might cause some wrong results. I will write a fix later.

OnTheThirdDay avatar Jul 04 '22 07:07 OnTheThirdDay