问题
I can't understand what this error means and apparently, no one ever got the same error on the internet
BadArgumentError: _MultiQuery with cursors requires
__key__
order
This happens here:
return SocialNotification.query().order(-SocialNotification.date).filter(SocialNotification.source_key.IN(nodes_list)).fetch_page(10)
The property source_key
is obviously a key and nodes_list
is a list of entity keys previously retrieved.
What I need is to find all the SocialNotifications
that have a field source_key
that match one of the keys in the list.
回答1:
The error message tries to tell you you that queries involving IN and cursors must be ordered by __key__
(which is the internal name for the key of the entity). (This is needed so that the results can be properly merged and made unique.) In this case you have to replace your .order()
call with .order(SocialNotification._key)
.
回答2:
I found the answer here: https://developers.google.com/appengine/docs/python/ndb/queries#cursors
You can change your query to:
SocialNotification.query().order(-SocialNotification.date, SocialNotification.key).filter(SocialNotification.source_key.IN(nodes_list)).fetch_page(10)
in order to get this to work. Note that it seems to be slow (18 seconds) when nodes_list is large (1000 entities), at least on the Development server. I don't have a large amount of test data on a test server.
回答3:
It seems that this also happens when you filter for an inequality and try to fetch a page.
(e.g. MyModel.query(MyModel.prop != 'value').fetch_page(...)
. This basically means (unless i missed something) that you can't fetch_page when using an inequality filter because on one hand you need the sort to be MyModel.prop
but on the other hand you need it to be MyModel._key
, which is hard :)
回答4:
I had the same error when filtering without a group. The error occurred every time my filter returned more than one result.
To fix it I actually had to add ordering by key.
回答5:
You need to the property you want to order on and key.
.order(-SocialNotification.date, SocialNotification.key)
来源:https://stackoverflow.com/questions/12449197/badargumenterror-multiquery-with-cursors-requires-key-order-in-ndb