Django的级联删除正在执行两次DELETE SQL

时间:2013-05-23 19:58:08

标签: python django django-models

我在这里有一个奇怪的情况,知道为什么myapp_entry_tag表的删除SQL正在执行两次?

>>> from myapp.models import Tag
>>> from django.db import connection
>>> from pprint import pprint
>>> Tag.objects.only('pk').get(pk=1).delete()
>>> pprint(connection.queries)
[{u'sql': u'SELECT `myapp_tag`.`id` FROM `myapp_tag` ''WHERE `myapp_tag`.`id` = 1 ',
  u'time': u'0.000'},
 {u'sql': u'DELETE FROM `myapp_entry_tag` WHERE `myapp_entry_tag`.`tag_id` IN (1)',
  u'time': u'0.000'},
 {u'sql': u'DELETE FROM `myapp_entry_tag` WHERE `myapp_entry_tag`.`tag_id` IN (1)',
  u'time': u'0.000'},
 {u'sql': u'DELETE FROM `myapp_tag` WHERE `id` IN (1)', u'time': u'0.000'}]

models.py:

class Tag(models.Model):
    ...

class EntryTag(models.Model):
    ...
    tag = models.ForeignKey(Tag)

1 个答案:

答案 0 :(得分:0)

(抱歉回答,但我无法评论你的问题)

使用django-toolbar获取查询执行的上下文。找到bug可能更容易。