'Cascaded deletions of m2m relations issue queries on the right database'

def test_m2m_deletion(self):

    dive = Book.objects.using(u'other').create(title=u'Dive    into    Python', published=datetime.date(2009, 5, 4))
    mark = Person.objects.using(u'other').create(name=u'Mark    Pilgrim')
    dive.authors = [mark]
    self.assertEqual(Person.objects.using(u'default').count(), 0)
    self.assertEqual(Book.objects.using(u'default').count(), 0)
    self.assertEqual(Book.authors.through.objects.using(u'default').count(), 0)
    self.assertEqual(Person.objects.using(u'other').count(), 1)
    self.assertEqual(Book.objects.using(u'other').count(), 1)
    self.assertEqual(Book.authors.through.objects.using(u'other').count(), 1)
    dive.delete(using=u'other')
    self.assertEqual(Person.objects.using(u'default').count(), 0)
    self.assertEqual(Book.objects.using(u'default').count(), 0)
    self.assertEqual(Book.authors.through.objects.using(u'default').count(), 0)
    self.assertEqual(Person.objects.using(u'other').count(), 1)
    self.assertEqual(Book.objects.using(u'other').count(), 0)
    self.assertEqual(Book.authors.through.objects.using(u'other').count(), 0)
    dive = Book.objects.using(u'other').create(title=u'Dive    into    Python', published=datetime.date(2009, 5, 4))
    dive.authors = [mark]
    self.assertEqual(Person.objects.using(u'default').count(), 0)
    self.assertEqual(Book.objects.using(u'default').count(), 0)
    self.assertEqual(Book.authors.through.objects.using(u'default').count(), 0)
    self.assertEqual(Person.objects.using(u'other').count(), 1)
    self.assertEqual(Book.objects.using(u'other').count(), 1)
    self.assertEqual(Book.authors.through.objects.using(u'other').count(), 1)
    mark.delete(using=u'other')
    self.assertEqual(Person.objects.using(u'default').count(), 0)
    self.assertEqual(Book.objects.using(u'default').count(), 0)
    self.assertEqual(Book.authors.through.objects.using(u'default').count(), 0)
    self.assertEqual(Person.objects.using(u'other').count(), 0)
    self.assertEqual(Book.objects.using(u'other').count(), 1)
    self.assertEqual(Book.authors.through.objects.using(u'other').count(), 0)
