Let's say I have a model:
class Pet(ndb.Model):
age = ndb.IntegerProperty(indexed=False)
name = ndb.StringProperty(indexed=True)
owner = ndb.KeyProperty(indexed=True)
And I have a list of keys named owners
. To do a query for Pet
s I would do:
pets = Pets.query(Pets.owner.IN(owners)).fetch()
The problem is that this query returns the whole entity.
How can I do a projected query and get just the owner and the name?
Or how should I structure the data to just get the name and the owner.
I can do a projection for the name
but I loose reference from the pet to the owner. And owner
can't be in the projection.
As you have noticed, you can't do that with the exact context you mentioned, because you hit one of the Limitations on projections:
Since
owner
is used in aIN
filter it can't be projected. Since you need theowner
and you can't project it you'll have to drop the projection and thus you'll always get the entire entity.One alternative would be to split your entity into 2 peer entities, always into a 1:1 relationship, using the same entity IDs:
This way you can do the same query, except on
PetA
kind instead of the originalPet
and the result you'd get would be the equivalent of the original projection query you were seeking.Unfortunately this will only work with one or a very few such projection queries for the same entity, otherwise you'd need to split the entity in too many pieces. So you may have to compromise.
You can find more details about the entity splitting in re-using an entity's ID for other entities of different kinds - sane idea?