Google App Engine: when you save an object that has reference properties, is the responsibility of maintaining the link integrity of the application?

when saving an object with reference properties, does the application respond to checking that the objects referenced by the link properties already exist in the data store? During unit testing with datastore_v3_stub, I found that the application engine would happily save the object A associated with it with the reference property B (and B does not yet exist in the data store). further, when A is saved, B is not saved. When you subsequently retrieve A from the data store and try to go to B, you get an exception. Is this expected behavior?

Code example:

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 

EDIT: I'm new to python, but is it possible to write a class that subclasses db.Model and overrides the put method to provide referential integrity (using some kind of reflection) before calling put db.Model. Then I can simply subclass this class to provide referential integrity in my model classes (e.g. A, B)

that's what i came up with. can any guru code revise this:

 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' 
+4
source share
1 answer

Yes. This is the app.

Quote:

As with a key value, a property reference value can refer to a data object that does not exist. If a deleted object is deleted from the data store, object references are not updated. Access to an entity that does not exist raises a ReferencePropertyResolveError reference.

taken from documents

+3
source

All Articles