Trying to model some highly connected, but also hierarchical data in app engine.
Here is an example:
Person:
Phone Numbers:
Number: 555-555-5555, Ext: 123, Notes: Work
Number: 444-444-4444, Ext: 456, Notes: Mobile
One entity, contained data structures stored as JSON blobs:
One way to do this would be to store the phone_numbers collection as an unindexed blob of JSON text, and then add a search property so that a person could be queried by phone number:
p_entity = Person()
p_entity.phone_numbers = dbText(simplejson.dumps([{'Number':'555-555-5555', 'Ext':'123', 'Notes':'Work'},{'Number':'444-444-4444', Ext:'456', Notes:'Mobile'}]))
p_entity.phone_numbers_search_property = ['5555555555', '4444444444']
p_entity.put()
Multiple entities with parent-child relationships:
Another way would be to use child and parent entities:
person_entity = Person()
person_entity.put()
phone_entity1 = PhoneNumber(parent=person_entity)
phone_entity.Number = '5555555555'
phone_entity.Ext = '123'
phone_entity.Notes = 'Work'
phone_entity2 = PhoneNumber(parent=person_entity)
phone_entity.Number = '4444444444'
phone_entity.Ext = '456'
phone_entity.Notes = 'Mobile'
A use case:
This is highly connected data. A person object contains multiple phone numbers. But phone calls can also be made to and from those phone numbers. Records of phone calls will also need to refer to these phone numbers.
The purpose of parent-child entity relationships:
After reading over the documentation, I was under the impression that the purpose of parent-child entity relationships was for performing transactions.
However, could they also be appropriate in this case? Is it almost as efficient to pull a parent and all of it’s children out of the datastore as to pull one entity out with its “children” instead stored as JSON text blobs?
Basic question
Is there a normal and accepted way to handle this kind of data in google app engine?
There’s no special bonus for pulling children entities out of the datastore. If you
gettwo entities, the cost is the same whether they’re in the same entity group or not. The only purpose of entity groups in app engine is transactions.Should your records of phone calls change when the phone number is changed? My initial thought is that the records should have a separate copy of the phone number data, not a reference to a phone number object. You can still query the call log by phone number. It makes more sense to store a reference to the contacts involved, so that if their name changes or something the call log can be updated.