Bulk get of child entities on Google app engine?
On Google App Engine in Python, I have a Visit entity that has a parent of Patient. A Patient may have multiple visits.
I need to set the mo开发者_开发知识库st_recent_visit (and some auxiliary visit data) somewhere for later querying, probably in another child entity that Brett Slatkin might call a "relationship index entity."
I wish to do so in a bulk style as follows:
1. get 100 Patient keys
2. get all Visits that have any of the Patients from 1 as an ancestor
3. go through the Visits and get the latest for each Patient
Is there any way to perform step 2 in a bulk get?
I know there is a way to bulk get entities from a list of keys, and there is a way to match a single entity by its ancestor.
There's no way in the datastore to to the kind of query you're looking for; it's a join, which isn't supported. Step 2 is going to require 100 queries.
You could store a list of Visit keys in each Patient entity, and then be able to do a simple bulk get all of these entities based on their keys.
Why not maintain a list of patient visit keys on the patient object? You will already know the last visit because it will be the last key added to the list.
My NF detector is going off like crazy which makes me think this is the right solution for the datastore.
If you set the property Patient
in model Visit
-- like this:
class Patient(db.Model):
pass
class Visit(db.Model):
patient_visited = db.ReferenceProperty(Patient)
there will be this automatic Query property visit_set
on each Patient entity, so you query like this:
for patient in Patient.all():
last_visit = patient.visit_set.order('-visited')
and it's all done. In just 101 queries. Pretty good.
精彩评论