'Foreign keys can cross databases if they two databases have a common source'

def test_foreign_key_cross_database_protection(self):

    pro = Book.objects.using('default').create(title='Pro    Django', published=datetime.date(2008, 12, 16))
    marty = Person.objects.using('default').create(name='Marty    Alchin')
    dive = Book.objects.using('other').create(title='Dive    into    Python', published=datetime.date(2009, 5, 4))
    mark = Person.objects.using('other').create(name='Mark    Pilgrim')
    try:
        dive.editor = marty
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEqual(marty._state.db, 'default')
    self.assertEqual(pro._state.db, 'default')
    self.assertEqual(dive._state.db, 'other')
    self.assertEqual(mark._state.db, 'other')
    dive.save()
    self.assertEqual(dive._state.db, 'default')
    try:
        Book.objects.using('default').get(title='Dive    into    Python').delete()
    except Book.DoesNotExist:
        self.fail('Source    database    should    have    a    copy    of    saved    object')
    dive = Book.objects.using('other').get(title='Dive    into    Python')
    self.assertEqual(dive._state.db, 'other')
    try:
        marty.edited = [pro, dive]
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEqual(marty._state.db, 'default')
    self.assertEqual(pro._state.db, 'default')
    self.assertEqual(dive._state.db, 'default')
    self.assertEqual(mark._state.db, 'other')
    try:
        Book.objects.using('default').get(title='Dive    into    Python').delete()
    except Book.DoesNotExist:
        self.fail('Source    database    should    have    a    copy    of    saved    object')
    dive = Book.objects.using('other').get(title='Dive    into    Python')
    self.assertEqual(dive._state.db, 'other')
    try:
        marty.edited.add(dive)
    except ValueError:
        self.fail('Assignment    across    master/slave    databases    with    a    common    source    should    be    ok')
    self.assertEqual(marty._state.db, 'default')
    self.assertEqual(pro._state.db, 'default')
    self.assertEqual(dive._state.db, 'default')
    self.assertEqual(mark._state.db, 'other')
    try:
        Book.objects.using('default').get(title='Dive    into    Python').delete()
    except Book.DoesNotExist:
        self.fail('Source    database    should    have    a    copy    of    saved    object')
    dive = Book.objects.using('other').get(title='Dive    into    Python')
    chris = Person(name='Chris    Mills')
    html5 = Book(title='Dive    into    HTML5', published=datetime.date(2010, 3, 15))
    self.assertEqual(chris._state.db, None)
    self.assertEqual(html5._state.db, None)
    self.assertEqual(dive._state.db, 'other')
    dive.editor = chris
    html5.editor = mark
    self.assertEqual(dive._state.db, 'other')
    self.assertEqual(mark._state.db, 'other')
    self.assertEqual(chris._state.db, 'default')
    self.assertEqual(html5._state.db, 'default')
    water = Book(title='Dive    into    Water', published=datetime.date(2001, 1, 1), editor=mark)
    self.assertEqual(water._state.db, 'default')
    cheesecake = mark.edited.create(title='Dive    into    Cheesecake', published=datetime.date(2010, 3, 15))
    self.assertEqual(cheesecake._state.db, 'default')
    (cheesecake, created) = mark.edited.get_or_create(title='Dive    into    Cheesecake', published=datetime.date(2010, 3, 15))
    self.assertEqual(cheesecake._state.db, 'default')
    (puddles, created) = mark.edited.get_or_create(title='Dive    into    Puddles', published=datetime.date(2010, 3, 15))
    self.assertEqual(puddles._state.db, 'default')
