0

I stumbled on this feature of App Engine's Datastore that you don't actually have to have a persisted root entity to have strongly consistent behavior work with GAE. You can use a computed key to store and load child entities. My question is: Is this a good practice or does this rely too much on an implementation quirk of the Datastore?

Here's an example using Python. This idiom will work with Java, too, I'm sure.

Let's say you have a child entity:

CustomerReport(ndb.Model):
  foo=ndb.StringProperty
  bar=ndb.FloatProperty
  #...

CustomerReports are generated based on a real entity type, Customer. But you can save with strong consistency this report entity by computing an ancestor key for a non-existent parent entity like this:

ReportRoot(ndb.Model):
  pass

Like so:

CustomerReport(parent=ndb.Key(ReportRoot, customer.key.id()), ... ).put()

Which can be retrieved again by simply computing the key:

CustomerReport.query(ancestor=ndb.Key(ReportRoot, customer.key.id())).fetch()

Thank you.

Nikolai
  • 56
  • 8

1 Answers1

2

Using a key as a parent that doesn't have a corresponding saved entity is perfectly fine. You don't even need to create a python class, just use the name as a string:

parent=ndb.Key('ReportRoot', some_id)

Your example, though, seems confused. What do you think you're gaining over just using the Customer's key as the Reports' parents?

Greg
  • 10,350
  • 1
  • 26
  • 35
  • I could use this technique to avoid locking the customer entity group when generating and reading reports. – Nikolai Sep 01 '14 at 19:53