0

I am working on an application using Google application engine and Django. I am using app engine patch. Some of the models have ReferenceProperty fields.

The issue is that when I am deleting some entries of the Referenced model, it should delete the entries where this ReferenceProperty is being used following the foreign key kind of relation. But actually it is not happening. The field remains without the deleted field and which is causing an error message:

ReferenceProperty failed to be resolved

Following is an example of the model:

class Topic(db.Model):
  title = db.StringProperty(required = True)
  body = db.TextProperty(required = True)
  category = db.ReferenceProperty(Category,required = True)
  status = db.StringProperty(default="open")
  creator = db.ReferenceProperty(User,required = True)

class Category(db.Model):
  name = db.StringProperty(required = True)
  creation_date = db.DateTimeProperty(auto_now_add=True)
  creator = db.ReferenceProperty(User,required = True)
  class Meta:
    verbose_name = "Category"
    verbose_name_plural = "Categories"
  def __unicode__(self):
    return '%s' % (self.name)

When I am deleting some of the categories, the related topics should also be deleted. But the topics are not deleted and cause the "ReferenceProperty failed to be resolved error" message.

Please suggest.

Thanks in advance.

bakkal
  • 54,350
  • 12
  • 131
  • 107
Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64

1 Answers1

1

This happens when the reference you are attempting to follow leads to a nonexistent entity - probably because you've already deleted it. Since you're trying to delete it anyway, you should simply catch and ignore this exception.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • Thanks for your reply nick. Actually the issue is, that I am deleting the records from the admin panel. But the records from the other associated models are not deleted which should have been deleted because of the ReferenceProperty relation. – Ankit Jaiswal Aug 10 '10 at 12:57
  • Reference property should follow the Foreign key constraints which it is not doing and only the parent record is deleted while the child record exists. – Ankit Jaiswal Aug 10 '10 at 12:59
  • ReferenceProperty is just syntactic sugar for storing a Key in the datastore. It doesn't attempt to enforce referential integrity. Nor is it the case that it would delete referenced entities even if it did - a reference does not imply ownership, and other records could be referencing that entity! – Nick Johnson Aug 10 '10 at 13:02
  • ok, but in the relational database this constraint is followed. Anyways, what should be the way to to get the desired result ? please suggest as I am in great need of it. – Ankit Jaiswal Aug 10 '10 at 13:19
  • _Some_ relational databases support this, but even then, only when it's explicitly enabled. App Engine doesn't have schemas, so even if the distributed nature of Bigtable didn't make this impractical, there'd still be nowhere to define it. What I'd suggest is what you're doing: have your code delete entities when they're no longer needed. If you get this error, it's because the entity in question has already been deleted, so you can safely ignore it. – Nick Johnson Aug 10 '10 at 15:40
  • Thanks a lot Nick. What I have implemented now, is to remove all the related entities if the ReferenceProperty is deleted and its working fine. Now the only issue I am getting is in overriding the delete method in the Django admin for the User. – Ankit Jaiswal Aug 11 '10 at 09:54