New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
querybuilder: qb.count() != len(qb.all()) #1600
Comments
@lekah do you know why? |
It's probably some unexpected behavior in the underlying ORM. |
Explanation of what happens: If I do this: qb = QueryBuilder()
qb.append(Node, filters={'id': 224276}, tag='c')
qb.append(Node, descendant_of='c')
#qb.append(Node, descendant_of='c')
#qb.append(Node, descendant_of='c')
print(qb.count())
print(qb.all()) In this case I get
which is expected (5 results, and indeed However if I uncomment one or two lines, I still get the same list, but I get Note that this is a Apparently, one should do q2 = qqq.distinct()
print(q2.count())
print(len(q2.all()) returns
|
Question for @lekah: is it safe/can we add always a |
No, I don't think we should add distinct by default. Maybe the user wants to see duplicates. In addition, the distinct doesn't really work that well for attributes or extras, or to clarify, it doesn't work the same way for different backends. |
As described above, apparently this is the behaviour of SQLAlchemy. Indeed, setting
would then return a list of 125 elements. To understand what the _has_mapper_entities really does. |
To understand the difference: s.query(dummy_model.DbNode).join(dummy_model.DbLink, dummy_model.DbLink.input_id==dummy_model.DbNode.id).filter(dummy_model.DbNode.id==224276).all() returns
(1 result) while s.query(dummy_model.DbNode.id).join(dummy_model.DbLink, dummy_model.DbLink.input_id==dummy_model.DbNode.id).filter(dummy_model.DbNode.id==224276).all() returns
(5 results), consistent with the code (the second would have a _has_mapper_entities to False because it asks for a id and not for an entity). |
That's actually what also the author of sqlalchemy says we should do, see this sqlalchemy/sqlalchemy#4143 (comment) |
We'll follow the advice given in sqlalchemy/sqlalchemy#4395 (comment) and use the hidden _has_member_entities flag to deduplicate results. |
…te rows. Therefore, the backend (psql) behavior is more closely reproduced.
Fixed in PR #2281 |
Just a comment for the future that there is work in progress to change the behaviour in SQLAlchemy, and this will appear in v2.0 of SQLAlchemy |
I just stumbled over this - probably an edge case because I am adding
Node
twice (was actually not intentional), but still the result is unexpected:Should this be fixed?
The text was updated successfully, but these errors were encountered: