Skip to content

Pagination with nested where problem? #4737

Closed

Description

@asuwend1

Hi,

I'm seeing a pagination problem (latest stable release):

Code:
models.merchants.findAndCountAll({
attributes: ['slug', 'name', 'description', ['image', 'imageFilename'], ['icon', 'iconFilename']],
include: [{model: models.promotions, order: ['priority', 'DESC'], attributes: ['priority','id','title','description'], include: [{model: models.events, attributes: ['id','start_at','end_at','special_event'], include: [{model: models.cities, attributes: [['slug', 'id'], 'city'], where:{} ]}]}],
offset: 0,
limit: 20, //expecting 20 items on a full page
subQuery: false
})

//yes, I am using where: {} so I can just replace {} to something else if I set a filter on the city

SQL command:
Executing (default): SELECT count(DISTINCT("merchants"."id")) AS "count" FROM "merchants" AS "merchants" LEFT OUTER JOIN "promotions" AS "promotions" ON "merchants"."id" = "promotions"."merchant_id" LEFT OUTER JOIN "events" AS "promotions.events" ON "promotions"."id" = "promotions.events"."promotion_id" INNER JOIN "cities" AS "promotions.events.city" ON "promotions.events"."city_id" = "promotions.events.city"."id";
Executing (default): SELECT "merchants"."id", "merchants"."slug", "merchants"."name", "merchants"."description", "merchants"."image" AS "imageFilename", "merchants"."icon" AS "iconFilename", "promotions"."priority" AS "promotions.priority", "promotions"."id" AS "promotions.id", "promotions"."title" AS "promotions.title", "promotions"."description" AS "promotions.description", "promotions.events"."id" AS "promotions.events.id", "promotions.events"."start_at" AS "promotions.events.start_at", "promotions.events"."end_at" AS "promotions.events.end_at", "promotions.events"."special_event" AS "promotions.events.special_event", "promotions.events.city"."id" AS "promotions.events.city.id", "promotions.events.city"."slug" AS "promotions.events.city.id", "promotions.events.city"."city" AS "promotions.events.city.city" FROM "merchants" AS "merchants" LEFT OUTER JOIN "promotions" AS "promotions" ON "merchants"."id" = "promotions"."merchant_id" LEFT OUTER JOIN "events" AS "promotions.events" ON "promotions"."id" = "promotions.events"."promotion_id" INNER JOIN "cities" AS "promotions.events.city" ON "promotions.events"."city_id" = "promotions.events.city"."id" LIMIT 20;

Result:
{
"total_records": 1203,
"total_pages": 61,
"current_page": 1,
"merchants": [ { ... }, ... ] //Only 12 items instead of expected 20 on this page!
}

Problem: The total records of 1203 is correct. However, on page 1 out of 61, I'm only seeing 12 merchants instead of the expected 20.

Any thought why is this happening/what am I doing wrong?

Thanks,
Ari

Activity

  1. asuwend1 commented on Nov 3, 2015

    @asuwend1
    Author

    Hi, was wondering if anybody have had a chance to look into this yet...

  2. mickhansen commented on Nov 3, 2015

    @mickhansen
    Contributor

    Honestly no, please format and try to reduce your issue, it's way too large for anything to quickly make sense of it.

  3. asuwend1 commented on Nov 4, 2015

    @asuwend1
    Author

    Hi Mick,

    Sorry, I didn't realize it's too large too analyze.
    Here's the simplified version of what works and what does not work (expecting 20 records on page 1 since the count is larger than the limit of 20).

    1 This works as it returns 20 rows
    Code:

    findObject =  {
        attributes: ['id'],
        include: [{model: models.promotions, attributes: ['id'], include: [{model: models.events, attributes: ['id'], where: {id: {$gt:400}}}]}],
        offset: 0,
        limit: 20
    };
    
    models.merchants.findAndCountAll(findObject).then(function(allMerchants) {
        res.json(allMerchants);
    });
    

    SQL:

    Executing (default): SELECT count(DISTINCT("merchants"."id")) AS "count" FROM "merchants" AS "merchants" LEFT OUTER JOIN "promotions" AS "promotions" ON "merchants"."id" = "promotions"."merchant_id" INNER JOIN "events" AS "promotions.events" ON "promotions"."id" = "promotions.events"."promotion_id" AND "promotions.events"."id" > 400;

    Executing (default): SELECT "merchants".*, "promotions"."id" AS "promotions.id", "promotions.events"."id" AS "promotions.events.id" FROM (SELECT "merchants"."id" FROM "merchants" AS "merchants" WHERE ( SELECT "merchant_id" FROM "promotions" AS "promotions" WHERE ("promotions"."merchant_id" = "merchants"."id") LIMIT 1 ) IS NOT NULL LIMIT 20) AS "merchants" LEFT OUTER JOIN "promotions" AS "promotions" ON "merchants"."id" = "promotions"."merchant_id" INNER JOIN "events" AS "promotions.events" ON "promotions"."id" = "promotions.events"."promotion_id" AND "promotions.events"."id" > 400;

    Result:
    {"count":1203,"rows":[{},...,{}]} //20 rows returned which is good

    2 This does not work as expected... (another level of nest in the include)
    Code:

    findObject =  {
        attributes: ['id'],
        include: [{model: models.promotions, attributes: ['id'], include: [{model: models.events, attributes: ['id'], include: [{model: models.event_outlets, attributes: ['id'], where: {id: {$gt:400}}}]}]}],
        offset: 0,
        limit: 20
    };
    
    models.merchants.findAndCountAll(findObject).then(function(allMerchants) {
        res.json(allMerchants);
    });
    

    SQL:

    Executing (default): SELECT count(DISTINCT("merchants"."id")) AS "count" FROM "merchants" AS "merchants" LEFT OUTER JOIN "promotions" AS "promotions" ON "merchants"."id" = "promotions"."merchant_id" LEFT OUTER JOIN "events" AS "promotions.events" ON "promotions"."id" = "promotions.events"."promotion_id" INNER JOIN "event_outlets" AS "promotions.events.event_outlets" ON "promotions.events"."id" = "promotions.events.event_outlets"."event_id" AND "promotions.events.event_outlets"."id" > 400;

    Executing (default): SELECT "merchants".*, "promotions"."id" AS "promotions.id", "promotions.events"."id" AS "promotions.events.id", "promotions.events.event_outlets"."id" AS "promotions.events.event_outlets.id" FROM (SELECT "merchants"."id" FROM "merchants" AS "merchants" LIMIT 20) AS "merchants" LEFT OUTER JOIN "promotions" AS "promotions" ON "merchants"."id" = "promotions"."merchant_id" LEFT OUTER JOIN "events" AS "promotions.events" ON "promotions"."id" = "promotions.events"."promotion_id" INNER JOIN "event_outlets" AS "promotions.events.event_outlets" ON "promotions.events"."id" = "promotions.events.event_outlets"."event_id" AND "promotions.events.event_outlets"."id" > 400;

    Result:
    {"count":921,"rows":[{}...{}]} //11 rows returned instead of the expected 20

    Any thought why the pagination is doing this? Or if there's any workaround for this?

    Thank you,
    Ari

  4. JSteunou commented on Nov 6, 2015

    @JSteunou
    Contributor

    It's kinda the same #4798 & #4716 where and order are sometimes on the all query whereas the limit is on the subquery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions