OneToOne fields are constrained to a single database
(self)
| 800 | self.assertEqual(owner_field.clean(mickey.pk, None), mickey.pk) |
| 801 | |
| 802 | def test_o2o_separation(self): |
| 803 | "OneToOne fields are constrained to a single database" |
| 804 | # Create a user and profile on the default database |
| 805 | alice = User.objects.db_manager("default").create_user( |
| 806 | "alice", "alice@example.com" |
| 807 | ) |
| 808 | alice_profile = UserProfile.objects.using("default").create( |
| 809 | user=alice, flavor="chocolate" |
| 810 | ) |
| 811 | |
| 812 | # Create a user and profile on the other database |
| 813 | bob = User.objects.db_manager("other").create_user("bob", "bob@example.com") |
| 814 | bob_profile = UserProfile.objects.using("other").create( |
| 815 | user=bob, flavor="crunchy frog" |
| 816 | ) |
| 817 | |
| 818 | # Retrieve related objects; queries should be database constrained |
| 819 | alice = User.objects.using("default").get(username="alice") |
| 820 | self.assertEqual(alice.userprofile.flavor, "chocolate") |
| 821 | |
| 822 | bob = User.objects.using("other").get(username="bob") |
| 823 | self.assertEqual(bob.userprofile.flavor, "crunchy frog") |
| 824 | |
| 825 | # Queries work across joins |
| 826 | self.assertEqual( |
| 827 | list( |
| 828 | User.objects.using("default") |
| 829 | .filter(userprofile__flavor="chocolate") |
| 830 | .values_list("username", flat=True) |
| 831 | ), |
| 832 | ["alice"], |
| 833 | ) |
| 834 | self.assertEqual( |
| 835 | list( |
| 836 | User.objects.using("other") |
| 837 | .filter(userprofile__flavor="chocolate") |
| 838 | .values_list("username", flat=True) |
| 839 | ), |
| 840 | [], |
| 841 | ) |
| 842 | |
| 843 | self.assertEqual( |
| 844 | list( |
| 845 | User.objects.using("default") |
| 846 | .filter(userprofile__flavor="crunchy frog") |
| 847 | .values_list("username", flat=True) |
| 848 | ), |
| 849 | [], |
| 850 | ) |
| 851 | self.assertEqual( |
| 852 | list( |
| 853 | User.objects.using("other") |
| 854 | .filter(userprofile__flavor="crunchy frog") |
| 855 | .values_list("username", flat=True) |
| 856 | ), |
| 857 | ["bob"], |
| 858 | ) |
| 859 |
nothing calls this directly
no test coverage detected