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

[@loopback/sequelize] BelongsTo relation sets target entity column name on the source entity #9591

Closed
KalleV opened this issue May 31, 2023 · 2 comments · Fixed by #10008
Closed
Labels

Comments

@KalleV
Copy link
Contributor

KalleV commented May 31, 2023

Describe the bug

I encountered an issue with a @BelongsTo relation adding column names to a SELECT query that did not exist on the source table when testing the sequelize extension. I traced it to this section:

const foreignKey = (
entityClass.definition.relations[key] as BelongsToDefinition
).keyTo;
sourceModel.belongsTo(targetModel, {
foreignKey: {name: foreignKey},
// Which client will pass on in loopback style include filter, eg. `include: ["thisName"]`
as: entityClass.definition.relations[key].name,
});

The "keyTo" assignment to the "foreignKey" causes the target entity column to be assigned back to the source entity by Sequelize leading to a broken query: https://github.com/sequelize/sequelize/blob/a9fd5010809366eb50fa9d6fc4bf0612a9d1d751/src/associations/mixin.js#L38. Looks like the fix is to swap the "keyTo" to "keyFrom" because the Sequelize belongsTo "foreignKey" is the source entity column name rather than the target.

- const foreignKey = (
-          entityClass.definition.relations[key] as BelongsToDefinition
-        ).keyTo;
+ const relation = entityClass.definition.relations[key] as BelongsToDefinition;
+ const foreignKey = relation.keyFrom;
+ const targetKey = relation.keyTo;
sourceModel.belongsTo(targetModel, {
  foreignKey: {name: foreignKey},
+ targetKey,

  // Which client will pass on in loopback style include filter, eg. `include: ["thisName"]`
  as: entityClass.definition.relations[key].name,
});

References:
https://sequelize.org/api/v6/class/src/model.js~model#static-method-belongsTo
https://loopback.io/doc/en/lb4/BelongsTo-relation.html#relation-metadata

Logs

No response

Additional information

No response

Reproduction

N/A

@KalleV KalleV added the bug label May 31, 2023
@shubhamp-sf
Copy link
Contributor

The insights you shared make sense to me but can you share details on how to reproduce it?
As of now the tests are present for belongsTo with the definition something like this: todo.model.ts#L25 is working.

@KalleV
Copy link
Contributor Author

KalleV commented Jun 18, 2023

It seems like the reproduction steps on #9617 apply to this one too.

KalleV added a commit to KalleV/loopback-next that referenced this issue Sep 4, 2023
…tion

Resolves: loopbackio#9591

Signed-off-by: KalleV <kvirtaneva@gmail.com>
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>
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment