data modeling - Google Datastore deletes and multiple parents -
i have data model in entity a
contains references 2 other entities, b
, c
. if either b
or c
deleted, want a
deleted.
when creating a
, it's possible name either b
or c
parent. possible name both b
, c
parents if either b
or c
deleted, a
deleted?
in more concrete terms, search results, result might have both category
, region
, web page birds in north america. result stored reference category , region. later, want delete category birds
, want result deleted. likewise, delete region north america
, want result deleted.
i hate go on @ such length such trivial scenario. doesn't seem covered in of datastore documentation. missing? flawed data model?
single-parent limitation:
a child can have 1 parent in datastore. in other words, a
can child of b
or c
, not both. of course, parent can have multiple children, though.
alternative:
you can use keyproperty
repeated=true
argument , store many entity keys on it. in python, this:
class a(ndb.model): associated_with = ndb.keyproperty(repeated=true) some_other_property = ndb.stringproperty() a_entity = a( associated_with = [b_key, c_key], some_other_property = 'any value' ) a_entity.put()
automatically triggering deletes:
datastore doesn't offer functionality out of box, can mimic in application. 1 idea implementing in python, example, extend model class own delete method (haven't tested code, it's illustration):
class a(ndb.model): associated_with = ndb.keyproperty(repeated=true) some_other_property = ndb.stringproperty() def delete_ext(entity): # entity object if entity.associated_with: associated in entity.associated_with: associated.delete() entity.key.delete()
you may want wrap deletes in transaction. beware single transaction can operate on 25 entity groups.
Comments
Post a Comment