Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #154 -- Fixed constraint error when deleting an object with a m…

…any-to-many field

git-svn-id: http://code.djangoproject.com/svn/django/trunk@447 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 991832d0c4dc211a4346256b62c44942894b495d 1 parent fd579f2
@adrianholovaty adrianholovaty authored
View
3  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)
View
11 tests/testapp/models/many_to_many.py
@@ -69,11 +69,18 @@ def __repr__(self):
>>> 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]
-
"""
Please sign in to comment.
Something went wrong with that request. Please try again.