'M2M relations can cross databases if the database share a source'

def test_m2m_cross_database_protection(self):

    pro = Book.objects.using('other').create(pk=1, title='Pro    Django', published=datetime.date(2008, 12, 16))
    marty = Person.objects.using('other').create(pk=1, name='Marty    Alchin')
    dive = Book.objects.using('default').create(pk=2, title='Dive    into    Python', published=datetime.date(2009, 5, 4))
    mark = Person.objects.using('default').create(pk=2, name='Mark    Pilgrim')
    pro.save(using='default')
    marty.save(using='default')
    dive.save(using='other')
    mark.save(using='other')
    self.assertEquals(Book.objects.using('default').count(), 2)
    self.assertEquals(Book.objects.using('other').count(), 2)
    self.assertEquals(Person.objects.using('default').count(), 2)
    self.assertEquals(Person.objects.using('other').count(), 2)
    try:
        marty.book_set = [pro, dive]
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEquals(marty._state.db, 'default')
    self.assertEquals(pro._state.db, 'default')
    self.assertEquals(dive._state.db, 'other')
    self.assertEquals(mark._state.db, 'other')
    self.assertEquals(Book.authors.through.objects.using('default').count(), 2)
    self.assertEquals(Book.authors.through.objects.using('other').count(), 0)
    Book.authors.through.objects.using('default').delete()
    try:
        marty.book_set.add(dive)
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEquals(marty._state.db, 'default')
    self.assertEquals(pro._state.db, 'default')
    self.assertEquals(dive._state.db, 'other')
    self.assertEquals(mark._state.db, 'other')
    self.assertEquals(Book.authors.through.objects.using('default').count(), 1)
    self.assertEquals(Book.authors.through.objects.using('other').count(), 0)
    Book.authors.through.objects.using('default').delete()
    try:
        dive.authors = [mark, marty]
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEquals(marty._state.db, 'default')
    self.assertEquals(pro._state.db, 'default')
    self.assertEquals(dive._state.db, 'other')
    self.assertEquals(mark._state.db, 'other')
    self.assertEquals(Book.authors.through.objects.using('default').count(), 2)
    self.assertEquals(Book.authors.through.objects.using('other').count(), 0)
    Book.authors.through.objects.using('default').delete()
    self.assertEquals(Book.authors.through.objects.using('default').count(), 0)
    self.assertEquals(Book.authors.through.objects.using('other').count(), 0)
    try:
        dive.authors.add(marty)
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEquals(marty._state.db, 'default')
    self.assertEquals(pro._state.db, 'default')
    self.assertEquals(dive._state.db, 'other')
    self.assertEquals(mark._state.db, 'other')
    self.assertEquals(Book.authors.through.objects.using('default').count(), 1)
    self.assertEquals(Book.authors.through.objects.using('other').count(), 0)
    alice = dive.authors.create(name='Alice')
    self.assertEquals(alice._state.db, 'default')
    (alice, created) = dive.authors.get_or_create(name='Alice')
    self.assertEquals(alice._state.db, 'default')
    (bob, created) = dive.authors.get_or_create(name='Bob')
    self.assertEquals(bob._state.db, 'default')
