Skip to content

Fix findAndCountAll issues - #5534

Merged
mickhansen merged 6 commits into
sequelize:masterfrom
sushantdhiman:fix-count-yarando
Mar 21, 2016
Merged

mickhansen merged 6 commits into
sequelize:masterfrom
sushantdhiman:fix-count-yarando

Conversation

@sushantdhiman

Copy link
Copy Markdown
Contributor

Fixed the subQuery generation which cause nested includes to be applied on main query. It also add other tests and fixes achieved due to this fix.

Fixes #4191, Fixes #3009, Fixes #5445
Closes #5530, Closes #5446, Closes #4378

May be it will close more issues, I will check and include tests in further PRs

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

@mickhansen @janmeier Please have a look on this one when you get chance

Comment thread changelog.md Outdated
@@ -1,4 +1,7 @@
# Future
- [Fixed] `findAndCountAll` generates wrong SQL query [#5445](https://github.com/sequelize/sequelize/issues/5445)
- [Fixed] `count` methods pollute the options.includes [#4191](https://github.com/sequelize/sequelize/issues/4191)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a change for this, have you just verified that it does indeed work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, more tests are awesome! Just wanted to check if it was already broken, and doesn't seem like it was

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it want broken because someone fixed .findAll method, But still there was no test. So its a verification only, it was already fixed

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

@mickhansen I have prefixed the condition, Can you somehow start the CI build, My local test run was ok.

@mickhansen

Copy link
Copy Markdown
Contributor

No CI active for this PR, very odd.

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

Yeah Travis didn't started any CI because last commit had [ci skip] flag, I have renamed the commit. CI will start now.

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

@mickhansen current CI has stuck, Can you please restart

@mickhansen

Copy link
Copy Markdown
Contributor

Please re-open and i'll make sure it gets a thorough review.

@mickhansen mickhansen self-assigned this Mar 16, 2016
@sushantdhiman sushantdhiman reopened this Mar 17, 2016
@mickhansen

Copy link
Copy Markdown
Contributor

Still no CI, very odd, no idea how to fix.

});
});
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you verifying in this test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its verification for #3009 , standalone commit 5d0f28a which add this test. While browsing through count and find all issues I came across this. Issue was already fixed so I included it in this PR

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

Hi @mickhansen , changed the condition and removed hasDuplicating as its not required now, rebased code, CI link here

@mickhansen

Copy link
Copy Markdown
Contributor

Looks great @sushantdhiman, could you change the test to use a lower limit than the count?

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

Let me know if this is correct behavior @mickhansen

SELECT Task.*,

Project.User.id AS Project.User.id, Project.User.name AS Project.User.name, Project.User.createdAt AS Project.User.createdAt, Project.User.updatedAt AS Project.User.updatedAt,

Tags.id AS Tags.id, Tags.name AS Tags.name, Tags.createdAt AS Tags.createdAt, Tags.updatedAt AS Tags.updatedAt,

Tags.TaskTag.id AS Tags.TaskTag.id, Tags.TaskTag.name AS Tags.TaskTag.name, Tags.TaskTag.createdAt AS Tags.TaskTag.createdAt, Tags.TaskTag.updatedAt AS Tags.TaskTag.updatedAt, Tags.TaskTag.TaskId AS Tags.TaskTag.TaskId,

Tags.TaskTag.TagId AS Tags.TaskTag.TagId FROM (
SELECT Task.id, Task.name, Task.createdAt, Task.updatedAt, Task.ProjectId, Project.id AS Project.id, Project.m AS Project.m, Project.createdAt AS Project.createdAt, Project.updatedAt AS Project.updatedAt, Project.UserId AS Project.UserId FROM Tasks AS Task INNER JOIN Projects AS Project ON Task.ProjectId = Project.id AND (Project.m = 'A') ORDER BY Task.id ASC LIMIT 1)
--- LIMIT APPLIED IN SUB QUERY

AS Task

INNER JOIN Users AS Project.User ON Project.UserId = Project.User.id AND (Project.User.name = 'user-name-2')

LEFT OUTER JOIN (TaskTags AS Tags.TaskTag INNER JOIN Tags AS Tags ON Tags.id = Tags.TaskTag.TagId) ON Task.id = Tags.TaskTag.TaskId

ORDER BY Task.id ASC; --- LIMIT SHOULD BE HERE ?

This means first we filter by LIMIT then apply JOINS on filtered IDs, Is this correct?

@mickhansen

Copy link
Copy Markdown
Contributor

The limit has to be on the inner query so it limits the rows we want to limit and not the cartesian product.
If a include has a where/required and it's not duplicating it can be moved to the subquery, if it's duplicating it needs to be on the outer query and add an additional subquery as filter to the subquery.

@sushantdhiman

Copy link
Copy Markdown
Contributor Author

@mickhansen If that example is correct way then great, I have changed the limit to 1.

I would like to mention that when we change order to ASC then test will fail, because we do filtering in sub-query first, this will get first i.e. task 1 (because limit is 1), then we check for project = "A" and user = "user-name-2" and task IN (1).

Since we have limited task id to 1, the main result is empty because required task ids 3, 4 (for project A and user user-name-2) were left out during sub query filter.

@mickhansen

Copy link
Copy Markdown
Contributor

@sushantdhiman Ordering shouldn't matter, a bug if it does.

mickhansen added a commit that referenced this pull request Mar 21, 2016
@mickhansen
mickhansen merged commit 7385a62 into sequelize:master Mar 21, 2016
@sushantdhiman
sushantdhiman deleted the fix-count-yarando branch March 21, 2016 12:03
@sushantdhiman

Copy link
Copy Markdown
Contributor Author

Thanks

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

Labels

None yet

Projects

None yet

2 participants