Repository navigation
Closed
Description
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, 10Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Wrong SQL generated when two level associations are required.[/-][+]Wrong SQL generated when two level associations are required with limit.[/+]igordeoliveirasa commentedon Nov 7, 2017
+1
igordeoliveirasa commentedon Nov 7, 2017
#8432
aegenet commentedon Nov 7, 2017
@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).
shwuhk commentedon Nov 7, 2017
@aegenet I have tried your fork and confirm that it is the same. The error is not fixed yet.
aegenet commentedon Nov 8, 2017
@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.
shtse8 commentedon Nov 8, 2017
@aegenet Yes, I have tried also, the bug still exist. Thanks for your contribution.
jeremybradbury commentedon Nov 21, 2017
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.
igordeoliveirasa commentedon Nov 23, 2017
@shwuhk Hi buddy. Any news? Thanks.
shtse8 commentedon Nov 23, 2017
I have implemented my own ORM already. Let's close the issue.
igordeoliveirasa commentedon Nov 28, 2017
I need this. Do not close. @shtse8
aegenet commentedon Nov 28, 2017
@igordeoliveirasa Have you tried using the "duplicating" option as suggested by @mjy78?
shtse8 commentedon Nov 28, 2017
@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 :)
aprilmintacpineda commentedon Feb 23, 2018
#9095
#8802
RazorDude commentedon Mar 23, 2018
+1
sushantdhiman commentedon Apr 7, 2018
Moved to #9166