GoogleCloudPlatform / datastore-ndb-python

Client library for use with Google Cloud Datastore from within the Google App Engine Python runtime.
https://cloud.google.com/appengine/docs/standard/python/ndb/
Apache License 2.0
114 stars 48 forks source link

Projection queries do not work on fields of a PolyModel subclass #261

Open cmoscardi opened 8 years ago

cmoscardi commented 8 years ago

Hi, the issue can be seen here on SO: http://stackoverflow.com/questions/28250761/appengine-datastore-polymodel-and-projection

I've copied the relevant bit here for convenience...

Below I present a simpler model (removed StructuredProperty) which gives the same issue:

class Contact(polymodel.PolyModel):
    telephone = ndb.StringProperty()

class Person(Contact):
    name = ndb.StringProperty()

This works:

qry = Person.query(projection=['telephone'])

This does not work:

qry = Person.query(projection=['name'])

In particular, the raised error is :

InvalidPropertyError: Unknown property name
pcostell commented 8 years ago

I took a look through the code. The main issue is that:

model._query passes in the result of _get_kind() to query, query then uses that to lookup the Model during projection/group_by property validation. In the case of polymodel, _get_kind() returns the base class name. Then when validation happens this will fail.

This looks like a broken separation of concerns. The model's _query() method should validate that the properties are a part of the query, not the query itself. However, this isn't quite right because of gql queries where the Kind is specified in the query and then constructed without calling model.query().

shobhitagarwal1612 commented 5 years ago

I am also stuck on this issue. It has been quite some time since the last comment but I am still wondering if anyone could help me. Thank you!