How to fix index error when querying GAE datastore?

后端 未结 2 753
傲寒
傲寒 2021-01-02 11:05

When I try to run a query on the datastore ordered by date I get the following error:

NeedIndexError: no matching index found.
The suggested index for this q         


        
2条回答
  •  旧时难觅i
    2021-01-02 11:31

    Thanks Lawrence, you got me on the right track- I think I have found the answer. The query must EXACTLY match the index definition.

    I discovered this by trying different GQL queries in the datastore admin box.

    For example, the following 2 queries:

    SELECT * FROM Message where ref='' and author='' order by date 
    SELECT * FROM Message where ref='' and author='' order by date asc 
    

    both fail with:

    no matching index found.
    
    The suggested index for this query is:
    - kind: Message
      properties:
      - name: author
      - name: ref
      - name: date
    

    However,

    SELECT * FROM Message where ref='' and author='' order by date desc
    

    succeeds. Likewise, queries which have less parameters than the index contains will also fail, eg:

    SELECT * FROM Message where ref='' order by date DESC
    

    fails with:

    no matching index found.
    
    The suggested index for this query is:
    - kind: Message
      properties:
      - name: ref
      - name: date
        direction: desc
    

    So the problem was in my query, the line:

    query = query.order(Message.date)
    

    is actually sorting in ascending order, but my index says DESCENDING order. The fix is:

    query = query.order(-Message.date)
    

提交回复
热议问题