Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@belongsTo with a default “keyTo” leads to a broken relation query #35

Closed
KalleV opened this issue May 29, 2023 · 3 comments
Closed

Comments

@KalleV
Copy link

KalleV commented May 29, 2023

Describe the bug
The belongsTo relation mapping from Loopback 4 to Sequelize has an edge case when using the default “keyTo” option.

References:
https://loopback.io/doc/en/lb4/BelongsTo-relation.html#relation-metadata

To Reproduce
Steps to reproduce the behavior:

  1. Create 2 Loopback entities (eg Book and Reader)
  2. Create a belongsTo relation in one of them such as:
@belongsTo(() => Reader, null, {
    type: 'string',
    required: false,
    length: 500
  })
  public readerId: string;
  1. Set the 2nd decorator argument to “null” or undefined
  2. Try to query the relation
  3. Observe a broken query to the relation (when using the DEBUG env variable) containing something like “readerreaderId”.

Expected behavior
Successful query when using the default options generated by Loopback for a belongsTo relation.

Additional context
I haven’t confirmed it but I assume the code in this section might need an update:

sourceModel.belongsTo(targetModel, {

Quick workaround is to explicitly set the “{ keyTo }” in the decorator options.

@shubhamp-sf shubhamp-sf self-assigned this May 29, 2023
@KalleV
Copy link
Author

KalleV commented May 31, 2023

I did some more investigation on this one and found this in the documentation for the default foreign key set by Sequelize:
https://sequelize.org/api/v6/class/src/model.js~model#static-method-belongsTo

The name of the foreign key attribute in the source table or an object representing the type definition for the foreign column (see Sequelize.define for syntax). When using an object, you can add a name property to set the name of the column. Defaults to the name of target + primary key of target

@RaghavaroraSF
Copy link
Contributor

Hii @KalleV
I have checked it via postgresql and mysql and it is showing correct data this is the resulting query
[
Screenshot from 2023-06-06 20-04-19

KalleV pushed a commit to KalleV/loopback-next that referenced this issue Jun 18, 2023
KalleV added a commit to KalleV/loopback-next that referenced this issue Jun 18, 2023
@KalleV
Copy link
Author

KalleV commented Jun 18, 2023

Ok looks like it wasn't quite that simple to replicate in isolation. To replicate it with minimal code, I had to introduce another belongsTo relation in another entity in addition to leaving out the relation keys in the decorator: KalleV/loopback-next@3c29d85

I filled in more details in the loopbackio/loopback-next#9617 issue's reproduction section.

KalleV added a commit to KalleV/loopback-next that referenced this issue Sep 4, 2023
Resolves: loopbackio#9591

Signed-off-by: KalleV <kvirtaneva@gmail.com>

test(sequelize): replicate duplicate column error with Sequelize belongsTo

Relates to:
 - loopbackio#9617
 - sourcefuse/loopback4-sequelize#35
@shubhamp-sf shubhamp-sf removed their assignment Sep 13, 2023
@shubhamp-sf shubhamp-sf closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2024
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

No branches or pull requests

3 participants