@@ -971,6 +971,19 @@ def test_foreign_key_cross_database_protection(self):
971
971
water = Book (title = "Dive into Water" , published = datetime .date (2001 , 1 , 1 ), editor = mark )
972
972
self .assertEquals (water ._state .db , 'default' )
973
973
974
+ # If you create an object through a FK relation, it will be
975
+ # written to the write database, even if the original object
976
+ # was on the read database
977
+ cheesecake = mark .edited .create (title = 'Dive into Cheesecake' , published = datetime .date (2010 , 3 , 15 ))
978
+ self .assertEquals (cheesecake ._state .db , 'default' )
979
+
980
+ # Same goes for get_or_create, regardless of whether getting or creating
981
+ cheesecake , created = mark .edited .get_or_create (title = 'Dive into Cheesecake' , published = datetime .date (2010 , 3 , 15 ))
982
+ self .assertEquals (cheesecake ._state .db , 'default' )
983
+
984
+ puddles , created = mark .edited .get_or_create (title = 'Dive into Puddles' , published = datetime .date (2010 , 3 , 15 ))
985
+ self .assertEquals (puddles ._state .db , 'default' )
986
+
974
987
def test_m2m_cross_database_protection (self ):
975
988
"M2M relations can cross databases if the database share a source"
976
989
# Create books and authors on the inverse to the usual database
@@ -1074,6 +1087,19 @@ def test_m2m_cross_database_protection(self):
1074
1087
self .assertEquals (Book .authors .through .objects .using ('default' ).count (), 1 )
1075
1088
self .assertEquals (Book .authors .through .objects .using ('other' ).count (), 0 )
1076
1089
1090
+ # If you create an object through a M2M relation, it will be
1091
+ # written to the write database, even if the original object
1092
+ # was on the read database
1093
+ alice = dive .authors .create (name = 'Alice' )
1094
+ self .assertEquals (alice ._state .db , 'default' )
1095
+
1096
+ # Same goes for get_or_create, regardless of whether getting or creating
1097
+ alice , created = dive .authors .get_or_create (name = 'Alice' )
1098
+ self .assertEquals (alice ._state .db , 'default' )
1099
+
1100
+ bob , created = dive .authors .get_or_create (name = 'Bob' )
1101
+ self .assertEquals (bob ._state .db , 'default' )
1102
+
1077
1103
def test_generic_key_cross_database_protection (self ):
1078
1104
"Generic Key operations can span databases if they share a source"
1079
1105
# Create a book and author on the default database
@@ -1150,6 +1176,13 @@ def test_generic_key_cross_database_protection(self):
1150
1176
review3 .content_object = dive
1151
1177
self .assertEquals (review3 ._state .db , 'default' )
1152
1178
1179
+ # If you create an object through a M2M relation, it will be
1180
+ # written to the write database, even if the original object
1181
+ # was on the read database
1182
+ dive = Book .objects .using ('other' ).get (title = 'Dive into Python' )
1183
+ nyt = dive .reviews .create (source = "New York Times" , content_object = dive )
1184
+ self .assertEquals (nyt ._state .db , 'default' )
1185
+
1153
1186
def test_subquery (self ):
1154
1187
"""Make sure as_sql works with subqueries and master/slave."""
1155
1188
# Create a book and author on the other database
0 commit comments