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.

limit fails when used with belongsToMany+hasMany association - even with separate: true

See original GitHub issue

Hi,

I’m not quite sure whether it is really a bug or I just did something wrong but I have some issues getting data from a belongsToMany association which itself has another hasMany association which I want to limit to only 1 result in one certain case.

I have a Product model which has many ProductImages and belongs to many ProductCategories. When I try to get all products of a certain category with only the first product image included, I get an error Invalid scope ‘overview’ called - overview is the scope I use to get only the first product image as preview.

If I get the product directly via Product.scope('overview').findAll() everything works fine. I’ve already tried separate: true but with no success.

I created a fully executable gist so you can have a look: https://gist.github.com/manuelbieh/b0ec0f41614cf9924b14

(MySQL Version here: https://gist.github.com/manuelbieh/a7a0aa14b3ef53e0bb78)

Any idea what I’ve done wrong and/or how it can be fixed? Or is it really a bug?

Maybe a short explanation of what I want to do to get some context: I have a page with a category page where a user can see all products of that particular category. For each product in that list I want to show the product’s first image as preview, so I really need only the first image. How can I realize that?

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
manuelbiehcommented, Oct 20, 2015

I might have found another bug related to the scoping bug: adding attributes to the scope causes another error:

TypeError: Cannot read property 'push' of undefined
    at node_modules\sequelize\lib\associations\has-many.js:324:64
    scopes: {
        overview: {
            include: [
                {
                    model: ProductImage,
                    required: false,
                    as: 'images',
                    attributes: ['id', 'filename'], // this line causes "Cannot read property 'push' ... "
                    limit: 1
                },
                {
                    model: ProductCategory,
                    required: false,
                    as: 'categories',
                    through: {
                        attributes: []
                    }
                }
            ],
        },
    }

Either limit or attributes can be used. If limit is set, attributes causes the error mentioned above. Leaving limit out, attributes works fine.

0reactions
manuelbiehcommented, Oct 27, 2015

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Limit on belongsToMany association with Sequelize
If you wish to get a certain user's friends limited by 2 friends (which ones? You have to add an order option) you...
Read more >
Eager Loading
In Sequelize, eager loading is mainly done by using the include option on a model finder query (such as findOne , findAll ,...
Read more >
Associations - Linking Tables Together - 3.10
CakePHP makes managing these associations easy. The four association types in CakePHP are: hasOne, hasMany, belongsTo, and belongsToMany. Relationship.
Read more >
The Comprehensive Sequelize Cheatsheet
A Sequelize instance must be created to connect to the database. By default, this connection is kept open and used for all the...
Read more >
Associations - Sequelize
BelongsTo; HasOne; HasMany; BelongsToMany ... When you create associations between your models in sequelize, foreign key references with constraints will ...
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