question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

instance.getThing() fails on many-to-many with non-primary target key

See original GitHub issue

Issue Description

What are you doing?

Here is the link to the SSCCE for this issue: sequelize-sscce#16

Summary:

Ship.belongsToMany(Captain, { through: 'foobar', sourceKey: 'name', targetKey: 'nickname' });
Captain.belongsToMany(Ship, { through: 'foobar', sourceKey: 'nickname', targetKey: 'name' });
// create some records ...
const ship = await Ship.findOne();
console.log(await ship.getCaptains()); // []

What do you expect to happen?

See the SSCCE, ship.getCaptains() should return something

What is actually happening?

See the SSCCE, ship.getCaptains() is returning an empty array

Additional context

This refers to the functionality added by #11311

Environment

  • Sequelize version: 5.21.1 (latest at the time of this writing)
  • Node.js version: v10.16.3

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fboechatscommented, Oct 21, 2019

@papb Sure, I can take this one.

0reactions
alissonmarquicommented, Apr 30, 2020

I think I have the same problem…

I have these relations:

Category.belongsToMany(models.Product, { as: 'products_children', targetKey: 'category_id', foreignKey: 'ancestor_id', otherKey: 'category_id', through: 'Categoryancestor'});

Product.belongsToMany(models.Category, { as: 'categories_ancestors', sourceKey: 'category_id',  foreignKey: 'category_id', otherKey: 'ancestor_id', through: 'Categoryancestor',});

When i call category.getProducts_children({ limit: 10});

SQL Generated:

#...
INNER JOIN "categories_ancestors" AS "Categoryancestor" ON "Product"."id" = "Categoryancestor"."category_id" AND "Categoryancestor"."ancestor_id" = 3 LIMIT 10
#...

SQL Expected:

#...
INNER JOIN "categories_ancestors" AS "Categoryancestor" ON "Product"."category_id" = "Categoryancestor"."category_id" AND "Categoryancestor"."ancestor_id" = 3 LIMIT 10
#...

The sequelize ignore my targetKey: ‘category_id’ and use the primary key.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specify non-primary target key in belongsToMany association
I am trying to configure a many to many relationship through a joining model. I want to use non-primary string key fields on...
Read more >
How to handle many-to-many relationship in Hibernate with ...
I have two objects - Role and Privilege. They have a many-to-many relationship. The code of each object is shown below. import java.io....
Read more >
Association Mapping - Doctrine Object Relational Mapper ...
This chapter explains mapping associations between objects. Instead of working with foreign keys in your code, you will always work with references to...
Read more >
Models - Django documentation
Each field in your model should be an instance of the appropriate Field class. ... foreign key to the target model (this would...
Read more >
Hibernate ORM 5.6.14.Final User Guide - Red Hat on GitHub
BasicType (a org.hibernate.type.Type specialization) instances keyed by a name. That is the purpose of the "BasicTypeRegistry key(s)" column in the previous ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found