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