neo4j-contrib / neomodel

An Object Graph Mapper (OGM) for the Neo4j graph database.
https://neomodel.readthedocs.io
MIT License
936 stars 231 forks source link

Incorrect query for count of objects #769

Closed OlegGustilov closed 8 months ago

OlegGustilov commented 9 months ago

Expected Behavior (Mandatory)

The method _count() frommatch.py should consider case to properly build the query with "skip" and "limit" clauses.

Actual Behavior (Mandatory)

The method _count() may build a non-working query with clauses "skip" and "limit". As result the Neo4J returns incorrect or empty (with "skip" clause) results.

How to Reproduce the Problem

In my case I am using Django framework and Django admin functionality. I created simple model and simple admin view to manage my objects for example Users list. When I trying to click next page using pagination in the Django Admin UI I see error "index out of range". It happens when Django trying to get count of items on the page, as result the method above will be called.

The same can happen in different cases without Django.

Simple Example

Datasets and Statements The method "_count()" may build following Neo4J query:

MATCH (user:User) RETURN count(user) SKIP 100 LIMIT 100

Such query returns empty results.

Screenshots (where it's possibile)

-

Specifications (Mandatory)

Currently used versions

Versions

mariusconjeaud commented 8 months ago

Hello,

Thanks for this interesting feedback.

So, the first idea that comes to mind (dropping SKIP and LIMIT when counting) is probably not the right solution, since your use case shows that we might still want to count items even when using skip and limit (for pagination purposes).

So the problem here is rather that instead of this query : MATCH (user:CTTermRoot) RETURN count(user) SKIP 100 LIMIT 100 => Returns nothing

It should generate this query : MATCH (user:CTTermRoot) WITH user SKIP 100 LIMIT 100 RETURN count(user) => Returns number of elements in your page

mariusconjeaud commented 8 months ago

Please review the PR : #771