Skip to content

Wrong SQL generated when two level associations are required with limit. #8602

Closed

Description

@shtse8

Assocations:

User hasMany Jobs
Job hasMany Shares
Share hasOne Block

Code in User Model

let jobs = await this.getJobs({
  attributes: ['id'],
  include: [{
    attributes: ['id'],
    association: 'shares',
    required: true,
    include: [{
      required: true,
      association: 'block'
    }]
  }],
  offset: 0,
  limit: 10
})

SQL Generated

 SELECT `Job`.*, 
       `shares`.`id` AS `shares.id` 
FROM   (SELECT `Job`.`id`, 
               `shares->block`.`id`                  AS `shares.block.id`, 
               `shares->block`.`shareId`             AS `shares.block.shareId`, 
               `shares->block`.`rewardTypeId`        AS 
               `shares.block.rewardTypeId`, 
               `shares->block`.`height`              AS `shares.block.height`, 
               `shares->block`.`hash`                AS `shares.block.hash`, 
               `shares->block`.`confirmationCount`   AS 
                      `shares.block.confirmationCount`, 
               `shares->block`.`rewardAmount`        AS 
               `shares.block.rewardAmount`, 
               `shares->block`.`difficulty`          AS 
               `shares.block.difficulty`, 
               `shares->block`.`createdAt`           AS `shares.block.createdAt` 
               , 
               `shares->block`.`settleStatus`        AS 
               `shares.block.settleStatus`, 
               `shares->block`.`settledAt`           AS `shares.block.settledAt` 
               , 
               `shares->block`.`status`              AS 
               `shares.block.status`, 
               `shares->block`.`expectedShareAmount` AS 
                      `shares.block.expectedShareAmount`, 
               `shares->block`.`totalShareAmount`    AS 
               `shares.block.totalShareAmount` 
        FROM   `Job` AS `Job` 
               INNER JOIN `Block` AS `shares->block` 
                       ON `shares`.`id` = `shares->block`.`shareId` 
        WHERE  `Job`.`userId` = 1
               AND (SELECT `jobId` 
                    FROM   `Share` AS `shares` 
                    WHERE  ( `shares`.`jobId` = `Job`.`id` ) 
                    LIMIT  1) IS NOT NULL 
        LIMIT  0, 10) AS `Job` 
       INNER JOIN `Share` AS `shares` 
               ON `Job`.`id` = `shares`.`jobId`; 

Result:

Unknown column 'shares.id' in 'on clause'

Reason:

Due to the limit, the join table statment misses the second level join in the subquery.
The proper sql should be:

SELECT `Job`.*, 
       `shares`.`id` AS `shares.id` 
FROM   (SELECT `Job`.`id`, 
               `shares->block`.`id`                  AS `shares.block.id`, 
               `shares->block`.`shareId`             AS `shares.block.shareId`, 
               `shares->block`.`rewardTypeId`        AS 
               `shares.block.rewardTypeId`, 
               `shares->block`.`height`              AS `shares.block.height`, 
               `shares->block`.`hash`                AS `shares.block.hash`, 
               `shares->block`.`confirmationCount`   AS 
                      `shares.block.confirmationCount`, 
               `shares->block`.`rewardAmount`        AS 
               `shares.block.rewardAmount`, 
               `shares->block`.`difficulty`          AS 
               `shares.block.difficulty`, 
               `shares->block`.`createdAt`           AS `shares.block.createdAt` 
               , 
               `shares->block`.`settleStatus`        AS 
               `shares.block.settleStatus`, 
               `shares->block`.`settledAt`           AS `shares.block.settledAt` 
               , 
               `shares->block`.`status`              AS 
               `shares.block.status`, 
               `shares->block`.`expectedShareAmount` AS 
                      `shares.block.expectedShareAmount`, 
               `shares->block`.`totalShareAmount`    AS 
               `shares.block.totalShareAmount` 
        FROM   `Job` AS `Job` 
##### Missed this join
               INNER JOIN `Share` AS `shares` 
                       ON `Job`.`id` = `shares`.`jobId` 
#####
               INNER JOIN `Block` AS `shares->block` 
                       ON `shares`.`id` = `shares->block`.`shareId` 
        WHERE  `Job`.`userId` = 1
               AND (SELECT `jobId` 
                    FROM   `Share` AS `shares` 
                    WHERE  ( `shares`.`jobId` = `Job`.`id` ) 
                    LIMIT  1) IS NOT NULL 
        LIMIT  0, 10) AS `Job` 
       INNER JOIN `Share` AS `shares` 
               ON `Job`.`id` = `shares`.`jobId` 

or much simplier one:

SELECT `Job`.`id`, 
  `shares`.`id`                         AS `shares.id`, 
  `shares->block`.`id`                  AS `shares.block.id`, 
  `shares->block`.`shareId`             AS `shares.block.shareId`, 
  `shares->block`.`rewardTypeId`        AS `shares.block.rewardTypeId`, 
  `shares->block`.`height`              AS `shares.block.height`, 
  `shares->block`.`hash`                AS `shares.block.hash`, 
  `shares->block`.`confirmationCount`   AS `shares.block.confirmationCount`, 
  `shares->block`.`rewardAmount`        AS `shares.block.rewardAmount`, 
  `shares->block`.`difficulty`          AS `shares.block.difficulty`, 
  `shares->block`.`createdAt`           AS `shares.block.createdAt`, 
  `shares->block`.`settleStatus`        AS `shares.block.settleStatus`, 
  `shares->block`.`settledAt`           AS `shares.block.settledAt`, 
  `shares->block`.`status`              AS `shares.block.status`, 
  `shares->block`.`expectedShareAmount` AS `shares.block.expectedShareAmount`, 
  `shares->block`.`totalShareAmount`    AS `shares.block.totalShareAmount` 
FROM   `Job` AS `Job` 
  INNER JOIN `Share` AS `shares` 
    ON `Job`.`id` = `shares`.`jobId` 
  INNER JOIN `Block` AS `shares->block` 
    ON `shares`.`id` = `shares->block`.`shareId` 
WHERE  `Job`.`userId` = 1
LIMIT  0, 10

Activity

  1. changed the title [-]Wrong SQL generated when two level associations are required.[/-] [+]Wrong SQL generated when two level associations are required with limit.[/+] on Nov 6, 2017
  2. igordeoliveirasa commented on Nov 7, 2017

    @igordeoliveirasa

    +1

  3. igordeoliveirasa commented on Nov 7, 2017

    @igordeoliveirasa
  4. aegenet commented on Nov 7, 2017

    @aegenet

    @shtse8 @igordeoliveirasa Can you try our fork for this issue ? https://github.com/konnecteam/sequelize/tree/develop

    I have to create a merge request, unfortunately I don't have time now (our fork containts Oracle pull request, so I need to create a new fork with only the fix).

  5. shwuhk commented on Nov 7, 2017

    @shwuhk

    @aegenet I have tried your fork and confirm that it is the same. The error is not fixed yet.

  6. aegenet commented on Nov 8, 2017

    @aegenet

    @shwuhk Indeed I did not read correctly, I settled the case of the "hasMany (hasOne)" and "hasOne (hasMany)", but that obviously did not correct this case "hasMany (hasMany (hasOne)), I will analyze when I have a few minutes.

  7. shtse8 commented on Nov 8, 2017

    @shtse8
    Author

    @aegenet Yes, I have tried also, the bug still exist. Thanks for your contribution.

  8. jeremybradbury commented on Nov 21, 2017

    @jeremybradbury

    I am getting a similar error with subQueries but it's lowercasing all of my camelCase field names which is causing the the error... I will take a snippet from the OP's above code and demonstrate my issue:

    `shares->block`.`rewardtypeid` AS `shares.block.rewardTypeId`,
    `shares->block`.`updatedat` AS `shares.block.updatedAt`,
    `shares->block`.`createdat` AS `shares.block.createdAt`,

    Fairly certain this has the same route issue... writing overly complex subQueries where fields don't match the main query's field names and insanely confusing aliases are created.

    Mine can't link the queries together because they are using the wrong aliases.

    In all my experience writing MySQL subqueries, I always wanted to avoid making aliases in the main query because the subquery runs before it and doesn't know your aliases from the outer query, because they haven't run yet. Perhaps this tip helps in some way?

    I think the real answer is avoiding subqueries when you don't need them. @shtse8 left a great example of a clean query with 2 joins.

  9. igordeoliveirasa commented on Nov 23, 2017

    @igordeoliveirasa

    @shwuhk Hi buddy. Any news? Thanks.

  10. shtse8 commented on Nov 23, 2017

    @shtse8
    Author

    I have implemented my own ORM already. Let's close the issue.

  11. igordeoliveirasa commented on Nov 28, 2017

    @igordeoliveirasa

    I need this. Do not close. @shtse8

  12. aegenet commented on Nov 28, 2017

    @aegenet

    @igordeoliveirasa Have you tried using the "duplicating" option as suggested by @mjy78?

  13. shtse8 commented on Nov 28, 2017

    @shtse8
    Author

    @igordeoliveirasa I don't think they will face this issue seriously. that's just waste of time for waiting the fix. So I have implemented my own ORM for my project already. BTW, I have reopened the issue for you. Hope you can get what you want :)

  14. reopened this on Nov 28, 2017
  15. aprilmintacpineda commented on Feb 23, 2018

    @aprilmintacpineda
  16. RazorDude commented on Mar 23, 2018

    @RazorDude

    +1

  17. sushantdhiman commented on Apr 7, 2018

    @sushantdhiman
    Contributor

    Moved to #9166

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions