Many-to-many table with identical foreign key is selecting just one value
See original GitHub issueHi 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:
- Created 7 years ago
- Reactions:1
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
@raducostea sorry you are right, i forgot to mention this