def test_m2m_cross_database_protection(self):
        # Create a book and author on the default database
        pro = Book.objects.create(title="Pro Django", published=datetime.date(2008, 12, 16))

        marty = Person.objects.create(name="Marty Alchin")

        # Create a book and author on the other database
        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")
        # Set a foreign key set with an object from a different database
        with self.assertRaises(ValueError):
            with transaction.atomic(using='default'):
                marty.edited.set([pro, dive])

        # Add to an m2m with an object from a different database
        with self.assertRaises(ValueError):
            with transaction.atomic(using='default'):
                marty.book_set.add(dive)

        # Set a m2m with an object from a different database
        with self.assertRaises(ValueError):
            with transaction.atomic(using='default'):
                marty.book_set.set([pro, dive])

        # Add to a reverse m2m with an object from a different database
        with self.assertRaises(ValueError):
            with transaction.atomic(using='other'):
                dive.authors.add(marty)

        # Set a reverse m2m with an object from a different database
        with self.assertRaises(ValueError):
            with transaction.atomic(using='other'):
                dive.authors.set([mark, marty])
Operations that involve sharing M2M objects across databases raise an error
dataset/ETHPy150Open django/django/tests/multiple_database/tests.py/QueryTestCase.test_m2m_cross_database_protection