My (python) app is using several entities, many of them in a 1:1 relationship. For example:
class Main(ndb.Model):
field1 = ndb.StringProperty()
#peer_key = ndb.KeyProperty(kind='Peer')
class Peer(ndb.model):
field2 = ndb.IntegerProperty()
#main_key = ndb.KeyProperty(kind='Main')
Some of the Main
entities may have a Peer
entity (created after the Main
entity) in exactly a 1:1 relationship.
I was thinking that at creation of the Peer
entity I could simply specify a numerical ID equal to the corresponding Main
entity's ID (auto-generated by the datastore and thus guaranteed to be unique):
main_entity = Main(field1='blah')
main_entity.put()
peer_entity = Peer(id=main_entity.key.id(), field2=10)
peer_entity.put()
This way I could significantly simplify my app's logic, without the need of storing and processing the entity keys to cross-reference these entities, especially when passing them across requests. For example, in a context where I have the main entity I could simply do:
peer_entity = Peer.get_by_id(main_entity.key.id())
Similarly, in a context where I have the peer entity:
main_entity = Main.get_by_id(peer_entity.key.id())
According to the documentation keys are just (kind, id)
pairs, meaning as long as the kinds are different and the ids are unique I shouldn't have problems. The tests I've done so far (on the development server) appear to be working fine.
Is my thinking correct or am I missing something (and was just lucky so far in my testing)?