1

Using parent child relationship where a parent can have children while each child has only one parent, does using Children.all().ancestor(parent.key) a good solution where a child is constructed by setting parent=parent.key in the constructor? Is the 1000 limit applies with this kind of query?

capecrawler
  • 67,353
  • 7
  • 31
  • 34
  • This question requires more context to be answered. Showing the models would help. – msw Mar 25 '10 at 12:12
  • FYI, Google has removed the 1000 limit: http://googleappengine.blogspot.com/2010/02/app-engine-sdk-131-including-major.html#links. Of course it's still questionable how much you can actually *do* with 1000 results before you hit the time limit, so you may find yourself using Cursors or another paging mechanism to apply a self-imposed limit. – Steve Jessop Mar 25 '10 at 12:13
  • hi Steve, That's good to hear and you're right, even if we can fetch more than 1000 records, we may also hit the time limit. – capecrawler Mar 25 '10 at 13:37

2 Answers2

2

The query returns what you'd expect, all Children which have the specified parent anywhere in their ancestry. The query expresses exactly that, so I doubt there's a simpler way of doing the same thing. But App Engine does keep adding features and surprising me :-)

Possibly you need parent.key(), I think it depends whether you're in Python or Java.

Btw, it's not recommended to use ancestor-parent-child to model relationships in your data. Entity groups exist to enable transactions, not for use as a "free" ReferenceProperty. parent-child should be a low-level implementation detail, meaning either "these two entities may need to be modified in a single transaction", or perhaps "I am playing an optimization trick which allows me to use list properties without having to load the list into memory when I get the entity". As a rule of thumb, if the entities don't all "belong" to the same user, then they shouldn't be parent-child related, because relating them in that way introduces contention when different users try modify them via different datastore nodes:

http://code.google.com/appengine/docs/python/datastore/keysandentitygroups.html#Entity_Groups_Ancestors_and_Paths

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • This talk gives a different perspective on your entity group rule of thumb with 'relational index entities': http://code.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html but I do understand your design philosophy. – dar Mar 25 '10 at 15:02
  • Yes, relation index entities are what I characterised as "an optimization trick to use list properties". I guess an ancestor filter is the natural way to find all the index entities for a given Message (or whatever), knowing that your hierarchy is only one level deep anyway? – Steve Jessop Mar 25 '10 at 17:32
  • just some clarification, when you said "As a rule of thumb, if the entities don't all "belong" to the same user, then they shouldn't be parent-child related,..." do you mean that in order to use ancestor-parent-child, all entity B should belong to one and only parent entity A and the parent entity A should be one and only of its kind?...... or I could have different instance of entity A that has its "own" entity B. – capecrawler Mar 26 '10 at 08:06
  • No, by "user" I mean what Google means in the link I gave, a human user of your app. It's not compulsory, but bear in mind that the way GAE distributes apps is to run them simultaneously on multiple nodes, migrate data around as needed, and use optimistic locking. Putting objects in the same entity group "ties them together", so that they share a lock, and that's how transactions can work. This limits how far your app can scale, and the whole point of GAE is to scale apps. A user is a good rule of thumb, because one user doesn't normally access the app from different nodes simultaneously. – Steve Jessop Mar 26 '10 at 16:17
  • ...so yes, you can (and should) have multiple A's each owning one or more B's. Just don't give all the A's a common parent, because if you do then every A and every B will all be in the same entity group. – Steve Jessop Mar 26 '10 at 16:41
2

Another way to get descendants (children) of a parent entity in Google App Engine that I just discovered:

childrenEntities = db.query_descendants(parentEntity).fetch(1000)

Not sure if it will be helpful to you. It was helpful to me because I was having difficulty figuring out how to access the child class(es) which were created with a python module I installed.

As others have indicated elsewhere, the 1000 limit for everything was removed in February 2010. See the linked blog entry for more details re: this.

Victor Van Hee
  • 9,679
  • 7
  • 33
  • 41