-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Bug when querying $relatedTo with limit and order by createdAt #6267
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
Comments
Can you please write a test case? |
I've attempted to recreate the issue with a fresh instance of Parse Server 3.9.0 and mongo 3.4.4 (using their respective docker images) with some new data and, so far, have been unable to do so. I'll take a closer look at the data I have that exhibits this issue and get back to you with more information. |
After failing to recreate the issue with a fresh instance, I took the broken data from my app and removed sections of it, bit by bit, to see if I could narrow down the culprit. I eventually got down to the two offending classes with the minimal amount of properties, and the issue was still present. At this point, I looked through the mongo data and discovered there were more rows in the join table than there were live objects. I cross-referenced the join table with the main tables and deleted the records that referenced objects which no longer existed - this solved the issue with my query. The problem is caused when deleting an object (via an SDK, the dashboard, or the REST api) without removing the object from the relation. The stale objects left in the relation causes problems when querying $relatedTo, setting a limit and ordering by createdAt. When I first started to work with relations, I confirmed that deleting the objects was sufficient to remove the object from its relation query results without throwing any errors. What I didn't notice was that some valid objects were being lost when I ordered the relatedTo queries by createdAt. What is the correct way to remove objects that are contained in relations of other objects? The documentation does not mention explicitly needing to remove relations before deleting objects. As this issue only seems to manifest when ordering by createdAt, is this a bug in parse server? Do I need to update my app to handle object deletions and relations explicitly? If the latter, then maybe all the Parse API guide documentation should be updated to stress this? If I need to update my app, I will have to create a script to sanitise my live database's join tables (to remove the stale objects). Is there anything in parse server that could maybe help with this? |
To reproduce, follow these steps:
You should now expect to see 14 instances of object B returned, but you will get 13. I did some digging and the following code seems to be the cause: DatabaseController.js - Line 1047 The limit and skip are being applied to the "relatedTo" query but only when sort is set and is createdAt. Removing this entire condition block seems to fix this issue (sorting and limiting works as expected). Not sure why you'd want limit and skip to be used on this query under this particular circumstance? |
Hi @cjlovescoffee . Thanks for going further with this. Do you want to open a PR with the proposed the change so the others can evaluate and merge? |
@cjlovescoffee I was able to reproduce this issue. You can check this PR for the reason why this exists. If you want you can remove sort by createdAt and this will fix your issue. We maybe able to fix your issue and keep the optimization but more investigation is needed. Edit: Here is a failing test. If you want you can use
|
Issue Description
When querying the objects in a relation property and setting both a limit and ordering by createdAt, not all expected objects are returned.
Removing the limit or changing the order by to another property seems to resolve the issue.
With 14 objects in the relation, the following incorrect number of objects are returned depending on the specified limit
I have reproduced this issue with Parse Server 3.1.3 and 3.9.0 running from the appropriate official docker images running on a linux and windows host machine running the docker image inside its linux container
Server
Database
The text was updated successfully, but these errors were encountered: