Skip to content

v4.0.0-1 Pagination support #6601

Closed
Closed

Description

@nareynolds

Hi,

Same problem with attempting pagination of a model with lots of associations... sorry!

We have an application to help manage intra-company shipments. We have a page to search shipments that are "en route" or "delivered". The search results are paginated (30 shipments per page). In the case below, we are searching for a shipment that has any items with barcode that contains "ABCDEFG."

When we execute something like...

Shipment.findAll({
   "where":{
      "status":[
         "en route",
         "delivered"
      ]
   },
   "include":[
      { model: Recipient },
      { model: Address },
      {
         model: Package,
         include: [
            {
               model: Item,
               "where":{
                  "barcode":{
                     "$iLike":"%ABCDEFG%"
                  }
               }
            }
         ]
      }
   ],
   "order":[["updatedAt", "DESC"]],
   "offset":0,
   "limit":30
})

... we get the some SQL like

SELECT "shipment".*, "packages"."id" AS "packages.id", ...
FROM (
    SELECT "shipment"."id", ...
    FROM "shipments" AS "shipment" 
    INNER JOIN "recipients" AS "recipient" ON "shipment"."recipientId" = "recipient"."id" 
    INNER JOIN "addresses" AS "address" ON "shipment"."addressId" = "address"."id" 
    WHERE "shipment"."status" IN ('en route', 'delivered') 
    AND (
        SELECT "shipmentId" 
        FROM "packages" AS "package" 
        WHERE ("package"."shipmentId" = "shipment"."id") 
        LIMIT 1
    ) IS NOT NULL 
    ORDER BY "shipment"."updatedAt" DESC 
    LIMIT 30 --Don't want this here
    OFFSET 0 --Don't want this here
) AS "shipment" 
INNER JOIN "packages" AS "packages" ON "shipment"."id" = "packages"."shipmentId" 
INNER JOIN "items" AS "packages.items" ON "packages"."id" = "packages.items"."packageId" AND "packages.items"."barcode" ILIKE '%ABCDEFG%' 
ORDER BY "shipment"."updatedAt" DESC;

This SQL returns nothing because that inner select statement has the limit on it. The purpose of an ORM is a abstract away the SQL, so that people who aren't SQL gurus can do advanced DB manipulations. Hence, I'd argue that the javascript code above should limit the number of shipments models that are returned. Something like...

SELECT *
FROM "shipments"
INNER JOIN "recipients" ON "shipments"."recipientId" = "recipients"."id"
INNER JOIN "addresses" ON "shipments"."addressId" = "addresses"."id"
INNER JOIN "packages" ON "packages"."shipmentId" = "shipments"."id"
INNER JOIN "items" ON "items"."shipmentId" = "shipments"."id"
WHERE "shipments"."id" IN (
    SELECT "id"
    FROM "shipments"
    WHERE EXISTS (
        SELECT 1 FROM "recipients" WHERE "id" = "shipments"."recipientId"
    ) AND EXISTS (
        SELECT 1 FROM "addresses" WHERE "id" = "shipments"."addressId"
    ) AND EXISTS (
        SELECT 1 FROM "packages" WHERE "shipmentId" = "shipments"."id"
    ) AND EXISTS (
        SELECT 1 FROM "items" 
        WHERE "shipmentId" = "shipments"."id"
        AND "items"."barcode" ILIKE '%ABCDEFG%'
    )
    ORDER BY "updatedAt" DESC
    OFFSET 0
    LIMIT 30
);

Granted, there is probably a more efficient way to structure that query.

I also wouldn't be opposed to adding this support thought an additional options property? For example:

Shipment.findAll({
  ...
  "page":1,
   "pageSize":30
})

Could you support this, pretty please?

Dialect: postgres
Database version: "PostgreSQL 9.4.5"
Sequelize version: 4.0.0-1

Activity

  1. felixfbecker commented on Sep 15, 2016

    @felixfbecker
    Contributor

    Seems like a bug to me, LIMIT and OFFSET are misplaced. I think there were some changes on master lately and a couple of similar issues open. Does the same happen with other versions (3.x, 4.0.0-0, 4.0.0-2)?

  2. nareynolds commented on Sep 16, 2016

    @nareynolds
    Author

    I just improved the suggest SQL above to be much more efficient.

  3. nareynolds commented on Sep 16, 2016

    @nareynolds
    Author

    This is still an issue on 4.0.0-2 :-(

  4. added
    type: bug
    on Sep 16, 2016
  5. overlookmotel commented on Oct 2, 2016

    @overlookmotel
    Contributor

    @nareynolds Can you please check if you have the same problem in v3.x? That would help narrow down where the bug has crept in.

  6. stale commented on Jun 29, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  7. edrpls commented on Jul 7, 2017

    @edrpls

    I have this problem on 3.30.2, is it still an issue on 4.2.1?

  8. mcranston18 commented on Jan 30, 2018

    @mcranston18

    This persists as of 4.4.2, though seems to have been folded into this issue #7344

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

    staletype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions