GQL query by key syntax no longer works in GAE console - google-app-engine

I have Datastore Viewer URLs stored for specific queries such as:
SELECT * FROM User WHERE __key__ = key('User',9999)
which now, in the updated console interface, don't work. Has the syntax changed?

It appears that the key() function has been changed. It worked for me by removing the single quotes, i.e.:
SELECT * FROM User WHERE __key__ = key(User,9999)

Related

App Engine no longer updating index.yaml

The index.yaml file of my GAE app is no longer updated by the development server.
I have recently added a new kind to my app and a handler that queries this kind like so:
from google.appengine.ext import ndb
class MyKind(ndb.Model):
thing = ndb.TextProperty()
timestamp = ndb.DateTimeProperty(auto_now_add=True)
and in the handler I have a query
query = MyKind.query()
query.order(-MyKind.timestamp)
logging.info(query.iter().index_list())
entities = query.fetch(100)
for entity in entities:
# do something
AFAIK, the development server should create an index for this query and update index.yaml accordingly. However, it doesn't. It just looks like this:
indexes:
# AUTOGENERATED
The logging.info(query.iter().index_list()) should output the index used for the query, it just says 'None'. Also, the SDK console says 'Datastore contains no indexes.'
Running the query returns the entities unsorted. I have two questions:
is there some syntax error in my code causes the query results be unsorted or is it the missing index?
if it's the missing index, is there a way to manually force the dev server to update index.yaml? Other suggestions?
Thank you
your call to order returns the new query..
query = MyKind.query()
query = query.order(-MyKind.timestamp)
..to clarify..
query.order(-MyKind.timestamp) does not change the query, it returns a new one, so you need to use the query returned by that method. As it is query.order(-MyKind.timestamp) in your code does nothing.

GQL Query Not Returning Results on StringProperty Query Test for Equality

class MyEntity(db.Model):
timestamp = db.DateTimeProperty()
title = db.StringProperty()
number = db.FloatProperty()
db.GqlQuery("SELECT * FROM MyEntity WHERE title = 'mystring' AND timestamp >= date('2012-01-01') AND timestamp <= date('2012-12-31') ORDER BY timestamp DESC").fetch(1000)
This should fetch ~600 entities on app engine. On my dev server it behaves as expected, builds the index.yaml, I upload it, test on server but on app engine it does not return anything.
Index:
- kind: MyEntity
properties:
- name: title
- name: timestamp
direction: desc
I try splitting the query down on datastore viewer to see where the issue is and the timestamp constraints work as expected. The query returns nothing on WHERE title = 'mystring' when it should be returning a bunch of entities.
I vaguely remember fussy filtering where you had to call .filter("prop =",propValue) with the space between property and operator, but this is a GqlQuery so it's not that (and I tried that format with the GQL too).
Anyone know what my issue is?
One thing I can think of:
I added the list of MyEntity entities into the app via BulkLoader.py prior to the new index being created on my devserver & uploaded. Would that make a difference?
The last line you wrote is probably the problem.
Your entities in the actual real datastore are missing the index required for the query.
As far as I know, when you add a new index, App Engine is supposed to rebuild your indexes for you. This may take some time. You can check your admin page to check the state of your indexes and see if it's still building.
Turns out there's a slight bug in the bulkloader supplied with App Engine SDK - basically autogenerated config transforms strings as db.Text, which is no good if you want these fields indexed. The correct import_transform directive should be:
transform.none_if_empty(str)
This will instruct App Engine to index the uploaded field as a db.StringProperty().

App Engine - GQL invalid query string

I don't know what can be wrong here:
SELECT * FROM RGUser    
WHERE isGuest = FALSE AND created < DATE('2011-09-01')    
ORDER BY created
screenshot http://my.jetscreenshot.com/3910/20110904-pwms-18kb.jpg
There's nothing wrong in your query, it's just the datastore viewer that does not work well in case of query modification after an error; just to copy the query, click again on Datastore viewer, paste it and launch it again.

Working with columns having dot(.) in their name using GQL

I use Objectify for datastore operations in my GAE/Java application. I have used Objectify's #Embeded facility in a couple of places in my project. Objectify automatically flattens the nested objects within the entity marked by #Embeded notation using the . separator. Thus I have ended up with column names like entity.embededObject.Field
For example I have an entity 'Person' in my data store with two columns name and address.email.
I want to filter through Person in the datastore viewer by writing a simple GQL query.
But the following query fails with a syntax error:
SELECT * FROM Person where address.email='mail#gmail.com'
whereas the following works as it should
SELECT * FROM Person where name='Joe'
What am I doing wrong?
GQL currently doesn't support this - only 'word' characters are supported. You should definitely file this as a bug in the issue tracker.
Tested today, it is possible to run the following with backquotes
SELECT * FROM `your.kind`
I believe this holds true for any parameter, but please correct me if I am wrong.

GQL query with numeric id in datastore viewer

I want to build GQL query to get an object using its numeric id. I'm doing this in Datastore viewer in App management console, so I can't use Model.get_by_id(numeric_id). Something like
SELECT * FROM Model WHERE id = <numeric_id>
also doesn't work.
Try this:
SELECT * FROM Model where __key__ = KEY('Model', <numeric_id>)
Unfortunately, there does not appear to be a way to write a query equivalent to
SELECT * FROM Model WHERE id = <numeric_id>
which would select all Model entities with the given id. If you're ok with something equivalent to
SELECT * FROM Model WHERE id = <numeric_id> AND parent IS NULL
you can use something like
SELECT * FROM Model where __key__ = KEY('Model', <numeric_id>)
If your entity does have a parent though, you'll need to specify that as part of the key, like
SELECT * FROM Model where __key__ = KEY('ParentModel', <parent_name_or_id>, 'Model', <numeric_id>)
If the parent itself has a parent, you'll need to specify that too. (Grandparent goes left of the parent, and so on.)
Of course if you're not restricted to GQL (like if you're using Python, Go, or Java), you can query the keys, decode them and filter by id, then fetch the corresponding entities. But of course that doesn't work in the Datastore Viewer since you can only use GQL.
Another way around is, first get the key for the entity using the id by
key = db.Key.from_path('Model', int(id))
then get the object by
obj = db.get(key)
The advantage is, you do not have to do any string formatting.
reference: problem set 3 at this course, https://classroom.udacity.com/courses/cs253/
I was getting this error:
GQL Query error: Encountered ... at line 1, column 42. Was expecting
one of: UNQUOTED_NAME ... QUOTED_NAME ..."
It turns out that in the Google AppEngine datastore developer's admin console, you should drop the quotes and use something like this:
SELECT * FROM MyEntity WHERE __key__ = Key(MyEntity, 5695872079757312)
In my case I had to change the type of ID from String to Long

Resources