How to delete an entity including all children

给你一囗甜甜゛ 提交于 2019-12-04 04:05:50

问题


I would like to do a cascading delete on an entity in the datastore. By this I mean all children and indirect children will also be deleted. I initially assumed this would be default behavior but somehow it is not...

My thought was something like this:

ndb.delete_multi(ndb.Model.query(ancestor=key).iter(keys_only = True))

But the Model should be a wildcard, because the entity can be the parent of several classes...

I would also like to delete BlobKeyProperties when deleting an entity. For this I was thinking about:

@classmethod
  def _post_delete_hook(cls, key, future):
  # inform someone they have lost a friend

which I should maybe use for cascading delete as well?


回答1:


For kindless ancestor queries create the query from the query class

ndb.delete_multi(ndb.Query(ancestor=key).iter(keys_only = True))

I wouldn't use the cascading delete for all child entities. If you have a lot then it will be much slower (unless you want to run the delete in a task).



来源:https://stackoverflow.com/questions/16576700/how-to-delete-an-entity-including-all-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!