On Mon, Dec 12, 2011 at 9:53 AM, Wilfred Hughes <wilf...@potatolondon.com>wrote:
> Following up on #17338 ([nonrel] supports_deleting_related_objects > database feature flag): > > carljm said that: > > > [Django] is (sort of) _emulating_ SQL cascading deletes, but it does so > in a way that doesn't assume anything at all from the backend. > > I'm not sure this is correct. If I define the following models: > > from django.db import models > > class Person(models.Model): > name = models.CharField(max_length=100) > > > class Car(models.Model): > name = models.CharField(max_length=100) > owner = models.ForeignKey(Person) > > Related objects are indeed deleted on a sqlite Django project: > > In [1]: from vortaro.models import Person, Car > > In [2]: p = Person.objects.create(name='bob') > > In [3]: c = Car.objects.create(name='robin reliant', owner=p) > > In [4]: p.delete() > > In [5]: Car.objects.all() > Out[5]: [] > > In [6]: from django.db import connection > > In [7]: connection.queries > Out[7]: > [{'sql': u'INSERT INTO "vortaro_person" ("name") VALUES (bob)', > 'time': '0.001'}, > {'sql': u'INSERT INTO "vortaro_car" ("name", "owner_id") VALUES > (robin reliant, 1)', > 'time': '0.001'}, > This SELECT: > {'sql': u'SELECT "vortaro_car"."id", "vortaro_car"."name", > "vortaro_car"."owner_id" FROM "vortaro_car" WHERE > "vortaro_car"."owner_id" IN (1)', > 'time': '0.001'}, > {'sql': u'DELETE FROM "vortaro_car" WHERE "id" IN (1)', 'time': > '0.000'}, > And the above subsequent delete of a votaro_car are Django finding the cars referencing the to-be-deleted Person and deleting them, before deleting the Person, which comes next. > {'sql': u'DELETE FROM "vortaro_person" WHERE "id" IN (1)', 'time': > '0.000'}, > {'sql': u'SELECT "vortaro_car"."id", "vortaro_car"."name", > "vortaro_car"."owner_id" FROM "vortaro_car" LIMIT 21', > 'time': '0.000'}] > > This isn't making a second query to deleting the Car model, it is > depending on the database to do this. Django did explicitly find-and-delete the Car. It did not rely on the underlying database to do that. > For comparison, here's the same > on App Engine: > > In [1]: from test_app.models import Person, Car > > In [2]: p = Person.objects.create(name='bob') > > In [3]: c = Car.objects.create(name='robin reliant', owner=p) > > In [4]: p.delete() > > In [5]: Car.objects.all() > Out[5]: [<Car: Car object>] > > In [6]: c = Car.objects.get() > > In [7]: c.owner > --------------------------------------------------------------------------- > DoesNotExist Traceback (most recent call > last) > /home/wilfred/bleeding_edge/django-testapp/<ipython-input-7- > f94c13ea1b0a> in <module>() > ----> 1 c.owner > > /home/wilfred/bleeding_edge/django-testapp/django/db/models/fields/ > related.pyc in __get__(self, instance, instance_type) > 313 rel_obj = rel_mgr.using(db).get(**params) > 314 else: > --> 315 rel_obj = > QuerySet(self.field.rel.to).using(db).get(**params) > 316 setattr(instance, cache_name, rel_obj) > 317 return rel_obj > > /home/wilfred/bleeding_edge/django-testapp/django/db/models/query.pyc > in get(self, *args, **kwargs) > 349 if not num: > 350 raise self.model.DoesNotExist("%s matching query > does not exist." > --> 351 % self.model._meta.object_name) > 352 raise self.model.MultipleObjectsReturned("get() > returned more than one %s -- it returned %s! Lookup parameters were > %s" > 353 % (self.model._meta.object_name, num, kwargs)) > > DoesNotExist: Person matching query does not exist. > > Have I horribly misunderstood? > > It seems the problem here is that Django's attempt to find the Cars referencing the to-be-deleted person is failing here, so it did not delete the associated Car. It would if it could find it though, really it's not using database cascade delete for this. Karen -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.