@@ -11,11 +11,15 @@ def create_geo_settings_for_existing_orgs(apps, schema_editor):
1111 Create OrganizationGeoSettings for all existing organizations
1212 that don't have one yet.
1313 """
14- Organization = apps . get_model ( "openwisp_users" , "Organization" )
14+ Organization = get_swapped_model ( apps , "openwisp_users" , "Organization" )
1515 OrganizationGeoSettings = get_swapped_model (apps , "geo" , "OrganizationGeoSettings" )
1616
17- for org in Organization .objects .iterator ():
18- OrganizationGeoSettings .objects .get_or_create (organization_id = org .pk )
17+ # Use the migration's DB alias so non-default databases are respected
18+ db_alias = schema_editor .connection .alias
19+ for org in Organization .objects .using (db_alias ).iterator ():
20+ OrganizationGeoSettings .objects .using (db_alias ).get_or_create (
21+ organization_id = org .pk
22+ )
1923
2024
2125def copy_estimated_location_enabled (apps , schema_editor ):
@@ -29,17 +33,20 @@ def copy_estimated_location_enabled(apps, schema_editor):
2933 )
3034 OrganizationGeoSettings = get_swapped_model (apps , "geo" , "OrganizationGeoSettings" )
3135
36+ # Use the migration's DB alias so non-default databases are respected
37+ db_alias = schema_editor .connection .alias
3238 # Iterate using iterator() to avoid loading all rows into memory.
33- for cfg in OrganizationConfigSettings .objects .iterator ():
34- geo , _ = OrganizationGeoSettings .objects .get_or_create (
39+ for cfg in OrganizationConfigSettings .objects .using ( db_alias ). iterator ():
40+ geo , _ = OrganizationGeoSettings .objects .using ( db_alias ). get_or_create (
3541 organization_id = cfg .organization_id
3642 )
3743 # Copy the value if different
3844 if getattr (geo , "estimated_location_enabled" , None ) != getattr (
3945 cfg , "estimated_location_enabled" , None
4046 ):
4147 geo .estimated_location_enabled = cfg .estimated_location_enabled
42- geo .save (update_fields = ["estimated_location_enabled" ])
48+ # Ensure save uses the same DB alias
49+ geo .save (update_fields = ["estimated_location_enabled" ], using = db_alias )
4350
4451
4552class Migration (migrations .Migration ):
0 commit comments