I am having trouble understanding how to structure an ancestor tree with several decedents. Suppose I have a model like this (every Entity has a Long id):
User
-Post
-Comment
Where the Comment is the grandchild of the User.
What is really annoying is to insert a Comment I need to generate the Key of the Post. And to generate the Key of the Post I also need to know the ID of the User:
Key<Post> postKey = Key.create(Key.create(User.class, userId), Post.class, postId);
This is a problem for me because when trying to insert a Comment into the datastore I need to also pass in the userId and postId just to generate the key of the Post.
Similarly, it is annoying to try and get a one Post because I need to pass in both the userId and postId to generate the Key.
I am looking for a better way to structure my model and API methods without having to pass in all those ancestor IDs to my methods. I was considering storing the websafeKey in every Post and Comment entity as a property like this:
String websafeKey = Key.create(Key.create(User.class, userId), Post.class, postId).getString();
Key<Post> key = Key.create(websafeKey);
Then I could the key to every Post and Comment (and other children of these Entities)) right there in the Entity. Then presumably I wouldn't have to pass in all those ancestor IDs in to my API methods all the time.
Not sure if that is a good idea though.
UserandPostdoes not necessarily have to be an ancestor one. It should suffice if thePostcontains a@Index Ref<User>. A couple of seconds eventual consistency really shouldn't matter since nobody can comment on a post that is not there yet. - konqiLongIDs andStringwebsafeKeys. By having@Index Ref<User>I could then make a query with a filter on that user object and get all posts from a user right? - MicroPostyou don't need the ancestor anymore. - konqi