From 991832d0c4dc211a4346256b62c44942894b495d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 10 Aug 2005 00:21:41 +0000 Subject: [PATCH] Fixed #154 -- Fixed constraint error when deleting an object with a many-to-many field git-svn-id: http://code.djangoproject.com/svn/django/trunk@447 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/meta/__init__.py | 3 +++ tests/testapp/models/many_to_many.py | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index 31a131d29d..52572053f1 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -775,6 +775,9 @@ def method_delete(opts, self): for rel_opts, rel_field in opts.get_all_related_many_to_many_objects(): cursor.execute("DELETE FROM %s WHERE %s_id=%%s" % (rel_field.get_m2m_db_table(rel_opts), self._meta.object_name.lower()), [getattr(self, opts.pk.name)]) + for f in opts.many_to_many: + cursor.execute("DELETE FROM %s WHERE %s_id=%%s" % (f.get_m2m_db_table(opts), self._meta.object_name.lower()), + [getattr(self, opts.pk.name)]) cursor.execute("DELETE FROM %s WHERE %s=%%s" % (opts.db_table, opts.pk.name), [getattr(self, opts.pk.name)]) db.db.commit() setattr(self, opts.pk.name, None) diff --git a/tests/testapp/models/many_to_many.py b/tests/testapp/models/many_to_many.py index 9b38b515b4..283a004ca4 100644 --- a/tests/testapp/models/many_to_many.py +++ b/tests/testapp/models/many_to_many.py @@ -69,11 +69,18 @@ True >>> p1.get_article_list(order_by=['headline']) [Django lets you build Web apps easily, NASA uses Python] -# If we delete an article, its publication won't be able to access it. +# If we delete a Publication, its Articles won't be able to access it. +>>> p1.delete() +>>> publications.get_list() +[Science News] +>>> a1 = articles.get_object(pk=1) +>>> a1.get_publication_list() +[] + +# If we delete an Article, its Publications won't be able to access it. >>> a2.delete() >>> articles.get_list() [Django lets you build Web apps easily] >>> p1.get_article_list(order_by=['headline']) [Django lets you build Web apps easily] - """