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.

Many-to-many table with identical foreign key is selecting just one value

See original GitHub issue

Hi guys, i have the following structure:

var User = sequelize.define('user', {
  name: DataTypes.STRING
});

var Post = sequelize.define('post', {
  text: DataTypes.STRING
});

var PostComment = sequelize.define('postComment ', {
  id: {
    type: DataTypes.BIGINT,
    primaryKey: true,
    autoIncrement: true
  },
  comment: DataTypes.TEXT
});

Post.belongsToMany(User, {as: 'postUserComment', through: {model: models.PostComment, unique: false}, foreignKey: 'idPost'});

User.belongsToMany(Post, {through: {model: models.PostComment, unique: false}, foreignKey: 'idUserComment'});

I am able to create multiples comments to the same post with an user.

But if i have more then one comment to the same post with the same user and try to select them by doing:

 Post.findAll({
          include: [{model: models.User, as: 'postUserComment', attributes:['name'], through: {attributes: ['comment']}},
          limit: 10,
          offset: 0,
          order: "id DESC"
...

it just return 1 comment for each user in a post. What do i have to do to select them all?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
dpmottcommented, Mar 14, 2017

My team has spent several days investigating odd behavior with the belongsToMany() relationship, where unique is set to false. We’ve written some tests to characterize the odd behaviors. See attached zip of a small project that demonstrates the issues.

Feedback on these tests is welcome and desired. If we’re misusing Sequelize, we’d like to know it.

Thanks.

nm-pg.zip

1reaction
ricardomachadosbcommented, Nov 25, 2016

@raducostea sorry you are right, i forgot to mention this

PostComment.belongsTo(models.User, {as: 'userComment', foreignKey: 'idUserComment'});
PostComment.belongsTo(models.Post, {as: 'postComment', foreignKey: 'idPost'});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize Many-to-many table with identical foreign key is ...
mysql - Sequelize Many-to-many table with identical foreign key is selecting just one value - Stack Overflow. Stack Overflow for Teams – Start ......
Read more >
Sequelize Many-to-many table with identical foreign key is ...
Coding example for the question Sequelize Many-to-many table with identical foreign key is selecting just one value-node.js.
Read more >
2 foreign keys in a many-to-many relationship
I'm designing a small database and ran into a situation where two foreign keys are in the same table referring to different rows...
Read more >
How to Add Multiple Foreign Keys to Same Table and Not Get ...
Articles explains how to add multiple foreign keys to same table in SQL Server using the SQL queries to add foreign keys. Learn...
Read more >
Many-to-many relationships
A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table. For example, a many-to-many ......
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