0

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
MrCooL
  • 926
  • 3
  • 15
  • 30
  • Try using something besides `type` as a variable name; it is a function provided by Python, and while there's nothing stopping you from changing it to something else (Python is very dynamic) you really should avoid doing it at all costs. – Adam Crossland May 17 '12 at 14:24
  • Also, does the query return results if you explicitly use the number `1` instead of a variable? – Adam Crossland May 17 '12 at 14:27
  • haha..After you asked me to change the 'type' variable, I tried explicitly by putting 1 as the query, but still return me empty results when I executed return query.count(limit=1) Any other suggestions ? Maybe I tried deploying again to double confirm – MrCooL May 17 '12 at 14:41
  • Here's what I tried more. I queried both voted_count and type properties, and both also returned me empty results. I'm really clueless of what went wrong. :( – MrCooL May 17 '12 at 15:16
  • Are you sure that your datastore still has data in it? With the development server, the local datastore may be recreated empty each time that you start up. – Adam Crossland May 17 '12 at 15:26
  • I actually tested it through appspot.com. I deployed it directly to App Engine. And I'm sure the data is in there, because when I retrieved all data without condition, it shows all the data , and there were 8 of them with type = 1 – MrCooL May 17 '12 at 17:02
  • You might have to show more code around the GqlQuery. Some important information is missing. – Adam Crossland May 17 '12 at 17:12
  • I've updated more. It's about the same as what I've posted originally. – MrCooL May 17 '12 at 17:40
  • Try this: `query = GqlQuery("SELECT __key__ FROM Feedback WHERE type = :1 ORDER BY date DESC", int(type_type))` if `type_type` isn't an integer, you won't get the expected results. – Adam Crossland May 17 '12 at 17:48
  • I've thought of that, and earlier, the type_type passed as parameter, really wasn't integer. And then I converted it when passed it as parameter. Anyway, to have double confirmation, I still tried your suggestion and it still didn't work. Remember, earlier I even tried explicitly putting 1, to replaced the type_type ? Even that didn't work. Which was why I'm clueless now. :( – MrCooL May 17 '12 at 17:56
  • I'm out of ideas, @MrCooL. Sorry. – Adam Crossland May 17 '12 at 18:10
  • I see that you have built quite a number of projects. My GQL certainly is correct, right ? It's sad to keep debugging something that is seem to be so right. The index is serving, syntax is correct, the parameter is integer. :( – MrCooL May 17 '12 at 18:17
  • Your GQL certainly looks correct to me; although, I always use the Query interface, personally. – Adam Crossland May 17 '12 at 18:20
  • What happens if you try it in the datastore admin page of the admin console? – Nick Johnson May 18 '12 at 00:10
  • @NickJohnson, I realized it seems that we have to query it a bit differently from Admin Console, could you give me example how to query it from Admin Console ? as in what to change from the syntax I'm using currently ? – MrCooL May 18 '12 at 09:04
  • Hi All, thanks for your help and suggestions. I appreaciated that very much especially to @AdamCrossland. I've figured out the solution, and shared it as answer. Thanks ! – MrCooL May 18 '12 at 12:24

1 Answers1

3

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.

MrCooL
  • 926
  • 3
  • 15
  • 30