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
This commit is contained in:
Adrian Holovaty 2005-08-10 00:21:41 +00:00
parent fd579f24d7
commit 991832d0c4
2 changed files with 12 additions and 2 deletions

View File

@ -775,6 +775,9 @@ def method_delete(opts, self):
for rel_opts, rel_field in opts.get_all_related_many_to_many_objects(): 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), 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)]) 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)]) cursor.execute("DELETE FROM %s WHERE %s=%%s" % (opts.db_table, opts.pk.name), [getattr(self, opts.pk.name)])
db.db.commit() db.db.commit()
setattr(self, opts.pk.name, None) setattr(self, opts.pk.name, None)

View File

@ -69,11 +69,18 @@ True
>>> p1.get_article_list(order_by=['headline']) >>> p1.get_article_list(order_by=['headline'])
[Django lets you build Web apps easily, NASA uses Python] [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() >>> a2.delete()
>>> articles.get_list() >>> articles.get_list()
[Django lets you build Web apps easily] [Django lets you build Web apps easily]
>>> p1.get_article_list(order_by=['headline']) >>> p1.get_article_list(order_by=['headline'])
[Django lets you build Web apps easily] [Django lets you build Web apps easily]
""" """