Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine Query (not filter) for children of an entity

Are the children of an entity available in a Query?

Given:

class Factory(db.Model):
    """ Parent-kind """
    name = db.StringProperty()

class Product(db.Model):
    """ Child kind, use Product(parent=factory) to make """
    @property
    def factory(self):
        return self.parent()
    serial = db.IntegerProperty()

Assume 500 factories have made 500 products for a total of 250,000 products. Is there a way to form a resource-efficient query that will return just the 500 products made by one particular factory? The ancestor method is a filter, so using e.g. Product.all().ancestor(factory_1) would require repeated calls to the datastore.

like image 685
Thomas L Holaday Avatar asked Feb 09 '09 15:02

Thomas L Holaday


1 Answers

Although ancestor is described as a "filter", it actually just updates the query to add the ancestor condition. You don't send a request to the datastore until you iterate over the query, so what you have will work fine.

One minor point though: 500 entities with the same parent can hurt scalability, since writes are serialized to members of an entity group. If you just want to track the factory that made a product, use a ReferenceProperty:

class Product(db.Model):
   factory = db.ReferenceProperty(Factory, collection_name="products")

You can then get all the products by using:

myFactory.products
like image 103
mcobrien Avatar answered Sep 18 '22 13:09

mcobrien