2

我在这里遇到了一个奇怪的情况,知道为什么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'}]

模型.py:

class Tag(models.Model):
    ...

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

1 回答 1

0

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

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

于 2013-07-06T18:42:24.150 回答