How can I test the validity of a ReferenceProperty

2019-07-12 15:03发布

I am currently testing a small application I have written. I have not been sufficiently careful in ensuring the data in my datastore is consistent and now I have an issue that I have some records referencing objects which no longer exist. More specifially, I have some objects which have ReferenceProperty's which have been assigned values; the objects referred to have been deleted but the reference remains.

I would like to add some checks to my code to ensure that referenced objects exist and acting accordingly. (Of course, I also need to clean up my data).

One approach is to just try to get() it; however, this should probably retrieve the entire object - I'd like an approach which just tests the existence of the object, ideally, just resulting in costs of key manipulations on the datastore, rather than full entity reads.

So, my question is the following: is there a simple mechanism to test if a given ReferenceProperty is valid which only involves key access operations (rather than full entity get operations)?

1条回答
男人必须洒脱
2楼-- · 2019-07-12 15:13

This will test key existence without returning an entity:

db.Query(keys_only=True).filter('__key__ =', test_key).count(1) == 1

I'm not certain that it's computationally cheaper than fetching the entity.

查看更多
登录 后发表回答