Search code examples
google-app-engineapp-engine-ndbgqlgqlquery

ndb query select both __key__ and properties simultaneously


Is it possible to select both the key as well as properties in an ndb query?

Right now I have:

one = ndb.gql('SELECT __key__ FROM Subject ORDER BY order ASC')
two = ndb.gql('SELECT name,order FROM Subject ORDER BY order ASC')

Is it possible to have something like:

oneandtwo = ndb.gql('SELECT __key__,name,order FROM Subject ORDER BY order ASC')
#This didn't work

If not, is it possible to combine one and two queries given that they are the same size?

oneandtwo = one + two
#This didn't work either

Thanks in advance!


Solution

  • Have you read the docs on projection queries and tried it ? https://cloud.google.com/appengine/docs/standard/python/ndb/projectionqueries

    Everything you need is in the documentation.

    If you perform a projection query on "name" for instance you will get objects back that include the key and the single property "name", if you used name and order you would get both of those properties. In addition the object has the key. However projection queries won't work if the matching index has not been built.

     > x = ndb.gql("SELECT name from Product").fetch(5)
     > x
    [<Product key_name="Key('Product', 'Pre-Filter-Sponge-15mm-BSP', 'Product', '002811-0001')"/>, <Product key_name="Key('Product', 'Pre-Filter-Sponge-15mm-BSP', 'Product', '002811-0002')"/>, <Product key_name="Key('Product', 'Pre-Filter-Sponge-25mm-BSP', 'Product', '002812-0002')"/>, <Product key_name="Key('Product', 'Pre-Filter-Sponge-25mm-BSP', 'Product', '002812-0003')"/>, <Product key_name="Key('Product', 'Seagull-Fibreglass-Spill', 'Product', '004321-0001')"/>]
     >  x[0]
    <Product key_name="Key('Product', 'Pre-Filter-Sponge-15mm-BSP', 'Product', '002811-0001')"/>
     >  x[0].name
    u'002811-0001'
     >  x[0].price
    Traceback (most recent call last):