Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle App Engine: while saving an entity that has reference properties, is it the app's responsibility to maintain referential integrity?
    text
    copied!<p>while saving an entity that has reference properties, is it the app's responsibility to check that the entities referred to by the reference properties already exist in the datastore? While unit testing with the datastore_v3_stub, I find that app engine will happily save a entity A that has a reference property B associated with it (and B does not exist in the datastore yet). further when A is saved, B is not saved. When you subsequently fetch A from the datastore, and try to navigate to B, you get an exception. Is this expected behavior? </p> <p>Example code: </p> <pre><code>user = MyUser(key_name='2', name='my user 2') e = db.get(user.key()) self.assertTrue(e is None) # user does not exist in datastore yet preferences = Preferences(user=user) # user is a ReferenceProperty preferences.put() e = db.get(user.key()) self.assertTrue(e is None) # user still does not exist in datastore e = db.get(preferences.key()) self.assertFalse(e is None) # but preferences were still stored e.user will give exception </code></pre> <p>EDIT: I am a python newbie, but is it possible to write a class that subclasses db.Model and overrides the put method to enforce referential integrity (by using some kind of reflection) before calling put of db.Model. Then I can just subclass this class to enforce referential integrity on my model classes (A, B above for example)</p> <p>this is what i came up with. can any gurus code-review this:</p> <pre><code>for name, property in obj.properties().items(): if isinstance(property, db.ReferenceProperty): try: value = getattr(essay, name) except datastore_errors.Error: print name, property, 'does not exist in datastore yet' continue key = value.key() o = db.get(key) if o is None: print name, property, value, 'does not exist in datastore yet' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload