In AppEngine's NDB datastore, it seems there are 2 ways to relate objects with one another. There's ndb.KeyProperty
and parents/ancestors keys. I'm a bit confused as to when I should be using one or the other?
Right now I've been using KeyProperty
exclusively, since it's the most familiar one, but I want to know when one is a better fit than the other.
Ancestors are hierarchical - they can be used when you have hierarchical relationships between things (for instance, in a forum system you might have Forum
s which have Topic
s which in turn have Post
s).
KeyProperty
is not inherently hierarchical - it just provides a link. It should be used for non-hierarchical linkages between items. Reusing the forum example from the previous paragraph, one might use a KeyProperty
to link a Post
to the User
who created it - because User
s aren't in the forum-topic-post hierarchy. They're related to all 3 (for instance, a User
might create a post, create a topic, and/or moderate a forum).
In the end, however, the main tradeoff between ancestors and keys is in consistency vs. throughput: ancestor-based queries give you strong consistency relative to recent updates, but impose a limit of 1 modification per second for any given entity group and a maximum size limit for the group due to lack of distribution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With