Question

Surprisingly, after having done a lot of queries without problem. I've run into the first strange GQL problem.

Following are the properties of a model called Feedback:

content  date    title   type    votes_count     written_by

and following are configured in index.yaml:

- kind: Feedback
  properties:
  - name: type
  - name: date
   direction: desc

When I queried for all Feedback data, sorted by date, it returns me all the results:

query = GqlQuery("SELECT __key__ FROM Feedback ORDER BY date DESC")

The type property is stored in type = db.IntegerProperty(default=1, required=False, indexed=True), and there are 8 rows of Feedback data with type of integer 1.

However, when I queried:

query = GqlQuery("SELECT __key__ FROM Feedback WHERE type = :1 ORDER BY date DESC", type)

It kept returning me empty results. What has gone wrong ?

Update

def get_keys_by_feedback_type(type_type):
    if type_type == FeedbackCode.ALL:
        query = GqlQuery("SELECT __key__ FROM Feedback ORDER BY date DESC")
    else:
        query = GqlQuery("SELECT __key__ FROM Feedback WHERE type = :1 ORDER BY date DESC", type_type)    
    return query

results = Feedback.get_keys_by_feedback_type(int(feedback_type_filter))
for feedback_key in results:
# iterate the query results

The index is serving:

Feedback 
type ▲ , date ▼ Serving
Was it helpful?

Solution

It was my bad for not describing clearly in the first place. I'm going to share the solution just in case, if somebody else faced the same problem.

The root cause of this was due to my insufficient knowledge of App Engine indexes. Earlier, the 'type' property was unindexed because I didn't plan to filter it until recent requirement changes.

Hence, I indexed the 'type' property from the property definition model as shown from the question. However, the 'type' property was remained unindexed for the reason explained from this, Indexing Formerly Unindexed Properties:

If you have existing records created with an unindexed property, that property continues to be unindexed for those records even after you change the entity (class) definition to make the property indexed again. Consequently, those records will not be returned by queries filtering on that property.

So, the solution would be :

To make a formerly unindexed property be indexed

  1. Set indexed=True in the Property constructor:

    class Person(db.Model):
        name = db.StringProperty()
        age = db.IntegerProperty(indexed=True)
    
  2. Fetch each record.

  3. Put each record. (You may need to change something in the record, such as the timestamp, prior to the put in order for the write to occur.)

So, there was nothing wrong with my GQL everything from the question. It was all because the 'type' property was remained unindexed. Anyway, still great thanks to @Adam Crossland for some insights and suggestions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top