Skip to content

PFQuery orQueryWithSubqueries not working #550

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

Closed
dcdspace opened this issue Feb 21, 2016 · 26 comments
Closed

PFQuery orQueryWithSubqueries not working #550

dcdspace opened this issue Feb 21, 2016 · 26 comments

Comments

@dcdspace
Copy link

In my iOS app, I have this line of code PFQuery *totalQuery = [PFQuery orQueryWithSubqueries:@[queryClosed, query, pendingQuery, openQuery]]; that used to work, but after adding the new client configuration to my own parse server, it doesn't execute. Is this a bug with parse server?

@tanmays
Copy link

tanmays commented Feb 21, 2016

Are any of those queries involving a relation field? It could be the same issue I'm facing. #432

@aryelevin
Copy link

Hey, I'm facing the same issue with iOS, while my console prints:
[Error]: invalid key name: $relatedTo (Code: 105, Version: 1.12.0)

Looks like an issue with the ParseServer.

@derickito
Copy link

I'm having a related issue and I think it's because the relation doesn't exist in my case. Does the relation field exist in your parse server database? For example if the relation field that you're querying on is "likes" for the user likes, that relation must be created first. I'm not sure how that's created though.
I think if you're migrating an app from parse where the relation is already created then this shouldn't happen. If it's a new app, then I haven't found a way of creating a relation.

@dcdspace
Copy link
Author

Yeah that is exactly what happens to me.

On Feb 21, 2016, at 5:58 PM, aryelevin notifications@github.com wrote:

Hey, I'm facing the same issue with iOS, while my console prints:
[Error]: invalid key name: $relatedTo (Code: 105, Version: 1.12.0)

Looks like an issue with the ParseServer.


Reply to this email directly or view it on GitHub.

@aryelevin
Copy link

Its a migrated app, so it shouldn't happen, Any news with it?

@nathan-savvy
Copy link

@gfosco any insight into the functionality of $relatedTo on the backend? I would love to contribute a pull request that adds support for $relatedTo.

@nathan-savvy
Copy link

Uncaught internal server error. { [MongoError: Can't canonicalize query: BadValue unknown top level operator: $relatedTo]
  name: 'MongoError',
  message: 'Can\'t canonicalize query: BadValue unknown top level operator: $relatedTo',
  '$err': 'Can\'t canonicalize query: BadValue unknown top level operator: $relatedTo',
  code: 17287 } MongoError: Can't canonicalize query: BadValue unknown top level operator: $relatedTo
    at Function.MongoError.create (/Users/NathanHillyer/Projects/Savvy/parse-local/savvy-parse-test/node_modules/mongodb-core/lib/error.js:31:11)
    at queryCallback (/Users/NathanHillyer/Projects/Savvy/parse-local/savvy-parse-test/node_modules/mongodb-core/lib/cursor.js:176:34)
    at Callbacks.emit (/Users/NathanHillyer/Projects/Savvy/parse-local/savvy-parse-test/node_modules/mongodb-core/lib/topologies/server.js:116:3)
    at null.messageHandler (/Users/NathanHillyer/Projects/Savvy/parse-local/savvy-parse-test/node_modules/mongodb-core/lib/topologies/server.js:282:23)
    at Socket.<anonymous> (/Users/NathanHillyer/Projects/Savvy/parse-local/savvy-parse-test/node_modules/mongodb-core/lib/connection/connection.js:273:22)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at TCP.onread (net.js:523:20)

@gfosco
Copy link
Contributor

gfosco commented Feb 26, 2016

$relatedTo is supported, at least partially (see ExportAdapter L455)... can you provide the rest formatted query you're submitting?

@nathan-savvy
Copy link

@gfosco

POST /parse/classes/_User HTTP/1.1
Host: ngrok -> localhost
User-Agent: -snip-
Connection: close
Content-Length: 1733
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-us
Connection: close
Content-Type: application/json; charset=utf-8
X-Forwarded-Proto: https
X-Parse-App-Build-Version: 25
X-Parse-App-Display-Version: 1.0
X-Parse-Application-Id: -snip-
X-Parse-Client-Key: -snip-
X-Parse-Client-Version: i1.12.0
X-Parse-Installation-Id: -snip-
X-Parse-Os-Version: 9.2.1 (13D15)
X-Parse-Session-Token: -snip-
X-Real-Ip: -snip-

{
   "where":{
      "gender":2,
      "objectId":{
         "$dontSelect":{
            "query":{
               "where":{
                  "$or":[
                     {
                        "objectId":"Txr5vFfp2h"
                     },
                     {
                        "$relatedTo":{
                           "key":"blockedUsers",
                           "object":{
                              "__type":"Pointer",
                              "className":"_User",
                              "objectId":"Txr5vFfp2h"
                           }
                        }
                     },
                     {
                        "objectId":{
                           "$select":{
                              "query":{
                                 "where":{
                                    "$or":[
                                       {
                                          "userA":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          },
                                          "userAResponse":1
                                       },
                                       {
                                          "userB":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          },
                                          "userBResponse":1
                                       }
                                    ]
                                 },
                                 "className":"MatchRelationshipActivityResponse"
                              },
                              "key":"userAObjectId"
                           }
                        }
                     },
                     {
                        "objectId":{
                           "$select":{
                              "query":{
                                 "where":{
                                    "$or":[
                                       {
                                          "userA":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          },
                                          "userAResponse":1
                                       },
                                       {
                                          "userB":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          },
                                          "userBResponse":1
                                       }
                                    ]
                                 },
                                 "className":"MatchRelationshipActivityResponse"
                              },
                              "key":"userBObjectId"
                           }
                        }
                     },
                     {
                        "objectId":{
                           "$select":{
                              "query":{
                                 "where":{
                                    "$or":[
                                       {
                                          "userA":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          }
                                       },
                                       {
                                          "userB":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          }
                                       }
                                    ],
                                    "match":true
                                 },
                                 "className":"MatchRelationship"
                              },
                              "key":"userAObjectId"
                           }
                        }
                     },
                     {
                        "objectId":{
                           "$select":{
                              "query":{
                                 "where":{
                                    "$or":[
                                       {
                                          "userA":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          }
                                       },
                                       {
                                          "userB":{
                                             "__type":"Pointer",
                                             "className":"_User",
                                             "objectId":"Txr5vFfp2h"
                                          }
                                       }
                                    ],
                                    "match":true
                                 },
                                 "className":"MatchRelationship"
                              },
                              "key":"userBObjectId"
                           }
                        }
                     }
                  ]
               },
               "className":"_User"
            },
            "key":"objectId"
         }
      },
      "birthday":{
         "$lte":{
            "__type":"Date",
            "iso":"1995-02-26T16:53:13.893Z"
         },
         "$gte":{
            "__type":"Date",
            "iso":"1972-02-26T16:53:13.894Z"
         }
      },
      "city":{
         "$inQuery":{
            "where":{
               "location":{
                  "$nearSphere":{
                     "__type":"GeoPoint",
                     "longitude":-77,
                     "latitude":38.54
                  },
                  "$maxDistance":0.01263008992624027
               }
            },
            "className":"City"
         }
      }
   },
   "_method":"GET"
}

@dasoga
Copy link

dasoga commented Mar 2, 2016

Hi all.
I got this error [Error]: improper usage of $dontSelect (Code: 102, Version: 1.12.0) when I request data in PFQueryTableViewController
Somebody know about it?
Thanks!

@flovilmart
Copy link
Contributor

@nathan-savvy can you try with that commit.

Your query seems to be very complex, and building a test just for it seems overkill but I'll try

@nathan-savvy
Copy link

@flovilmart Thank you, I will try it on Friday and let you know how it goes!

@dcdspace
Copy link
Author

dcdspace commented Mar 3, 2016

@flovilmart I am still receiving an error when trying to run an or query, after updating to the latest verion of Parse Server: invalid key name: $relatedTo. Also thanks for your great support to the repo!

@flovilmart
Copy link
Contributor

@dcdspace I'm onto it, can you give me the js/swift/objc-c code for your query? It seems to be occurring on very complex queries

@flovilmart
Copy link
Contributor

@nathan-savvy I've reproduced the query you're running with the master branch:


    "objectId": {
        "$notInQuery": {
            "where": {
                "$or": [
                    {
                        "objectId": "lxxELrq7lY"
                    },
                    {
                        "$relatedTo": {
                            "object": {
                                "__type": "Pointer",
                                "className": "_User",
                                "objectId": "lxxELrq7lY"
                            },
                            "key": "blockedUsers"
                        }
                    },
                    {
                        "objectId": {
                            "$select": {
                                "key": "userAObjectId",
                                "query": {
                                    "where": {
                                        "$or": [
                                            {
                                                "userA": {
                                                    "__type": "Pointer",
                                                    "className": "_User",
                                                    "objectId": "lxxELrq7lY"
                                                },
                                                "userAResponse": 1
                                            },
                                            {
                                                "userB": {
                                                    "__type": "Pointer",
                                                    "className": "_User",
                                                    "objectId": "lxxELrq7lY"
                                                },
                                                "userBResponse": 1
                                            }
                                        ]
                                    },
                                    "className": "MatchRelationshipActivityResponse"
                                }
                            }
                        }
                    },
                    {
                        "objectId": {
                            "$select": {
                                "key": "userBObjectId",
                                "query": {
                                    "where": {
                                        "$or": [
                                            {
                                                "userA": {
                                                    "__type": "Pointer",
                                                    "className": "_User",
                                                    "objectId": "lxxELrq7lY"
                                                },
                                                "userAResponse": 1
                                            },
                                            {
                                                "userB": {
                                                    "__type": "Pointer",
                                                    "className": "_User",
                                                    "objectId": "lxxELrq7lY"
                                                },
                                                "userBResponse": 1
                                            }
                                        ]
                                    },
                                    "className": "MatchRelationshipActivityResponse"
                                }
                            }
                        }
                    }
                ]
            },
            "className": "_User"
        }
    }
}

And it seems to run properly, not throwing error anymore.

@nathan-savvy
Copy link

@flovilmart You're the man. Much appreciated.

@flovilmart
Copy link
Contributor

I take that as it's working now?

@nathan-savvy
Copy link

Sorry, I mean thank you for testing the query. I won't be able to give it an official test until tomorrow (busy week).

@flovilmart
Copy link
Contributor

no problemo

@dcdspace
Copy link
Author

dcdspace commented Mar 4, 2016

@flovilmart - thanks again

`
PFRelation *myPendingJobs = [profilePerson relationForKey:@"pendingJobs"];

        PFQuery *pendingQuery = [myPendingJobs query];

        [pendingQuery whereKey:@"open" equalTo:[NSNumber numberWithBool:YES]];         

        PFQuery *openQuery = [PFQuery queryWithClassName:@"Jobs"];

        [openQuery whereKey:@"User" equalTo:[PFUser currentUser]];

        currentPerson = objects[0];

        PFRelation *acceptedJobsRelation = [currentPerson relationForKey:@"acceptedJobs"];

        PFQuery *query = [acceptedJobsRelation query];            

        PFQuery *totalQuery = [PFQuery orQueryWithSubqueries:@[query, pendingQuery, openQuery]];`

@flovilmart
Copy link
Contributor

Is that resolved for everyone running on master?

@nathan-savvy
Copy link

@flovilmart This resolved our issue. Thanks again.

@aryelevin
Copy link

@flovilmart This solved ours also! THANKS A LOT!!!!!!

@jaimeagudo
Copy link

Hi guys, on parse-server 2.4.2 I am experiencing a very similar issue, you might reproduce it with the following snippet

let alreadyDiscarded = object.relation('discards').query()
let alreadyLiked = object.relation('likes').query()

alreadyDiscarded._orQuery([alreadyLiked]).find({useMasterKey: true}).then(function(result){
  ...
})

I got exactly the same error

{"error":{"code":105,"message":"Invalid key name: $relatedTo"} }

the original intention was to use the or wrapper as in

Parse.Query.or(alreadyDiscarded, alreadyLiked).find({useMasterKey: true}).then(

but I realised that when one of the relations is null the classname check rises an exception Error: All queries must be for the same class. as per https://github.com/parse-community/Parse-SDK-JS/blob/master/src/ParseQuery.js#L1113

I think an empty relation (and therefore an empty result set) is a feasible use case that should be supported. Am I missing something guys?

Many thanks for reading

@jaimeagudo
Copy link

Anything wrong with my attempt @flovilmart? If I am right about my assumptions I am happy to submit a PR to fix it

@flovilmart
Copy link
Contributor

@jaimeagudo you're more than encouraged to submit a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants