2

I was looking for examples wherein in GQL where I can select attributes besides ID and filter by ID as well.

The pseudo code of what I want to accomplish is something like this:

myResults = db.GQLQuery("select __KEY__, ATTR1, ATTR2 from MyTABLE" 
                            "WHERE __key__ > KEY('MyTable',:LAST_ID)
                            "order by ID DESC LIMIT 100")

I saw some examples wherein I get the Key, but I need to iterate over the keys to get the record.

Is there a way for me to get the record collection based on the KEY/ID criteria?

Thanks In Advance

user1570124
  • 31
  • 1
  • 1

1 Answers1

0

The way you're using __key__ implies that all keys for new records are greater than those created previously (which is not the case). If you want to filter by creation time, you'll need to add a field for this, set upon creation of the record.

Dan Holevoet
  • 9,183
  • 1
  • 33
  • 49
  • No I need unique IDS for my query result collection. :LAST_ID may be misleading. In this case I refer to fetch all record after the previously stored id – user1570124 Feb 15 '13 at 03:23
  • Yes, I interpreted that correctly, and my answer still stands. You can't filter by __key__ > X in order to get newer records. A given record's __key__ may be greater or less than any other record, independent of the time they were created. If you want to filter by creation time, you need to use a different property. – Dan Holevoet Feb 15 '13 at 19:28
  • That is fine. I can live with the fact that IDs may not be temporally incremental. As long as I don't get previously fetched ID I am good. This is a very basic SQL requirement. Maybe I am missing something - but I do not know why Google made it some convoluted. Why would the key contain table name, ID. Why only having ID not suffice. And why should I not be able to fetch based on ID. Why should it be treated differently from other attributes (other than the fact that is indexed, unique etc). Thanks & Regards – user1570124 Feb 15 '13 at 21:07
  • What you're asking for is becoming less clear. If you just want to fetch items (including keys) and just want to make sure you're not fetching items you've already retrieved, you should use a query and a cursor: https://developers.google.com/appengine/docs/python/datastore/queries#Query_Cursors. – Dan Holevoet Feb 15 '13 at 21:42
  • Sorry for the confusion. Yes I can do as how the link suggests, but I need the IDs as well - since they will be used as a foreign key in another table or in the same table (in some cases where in I need hierarchy relationships). What I do not understand is if I CAN obtain Key (and thus ID) and the attributes - why does not google allow me to obtain them in a single query. I do not see a technical reason why they should prevent users from doing so. – user1570124 Feb 15 '13 at 22:51
  • Yes, you can obtain the key (as you specified in your original code). See https://developers.google.com/appengine/docs/python/datastore/gqlreference – Dan Holevoet Feb 20 '13 at 04:03
  • Thanks for the link. But I do not see where I can obtain both the __key__ and the attributes. I can obtain the keys alone as given in the link below:SELECT __key__ FROM Person WHERE age = NULL. If I try to fetch both the __KEY__ and attributes I get the error: BadRequestError: projections are not supported for the property: __key__. Thanks for your help! – user1570124 Mar 05 '13 at 23:40