Repository: openwisp/openwisp-controller (language: python)

Issue:
[change] Remove protection on BaseDeviceLocation.floorplan and location
Remove BaseDeviceLocation protection on foreign keys while double checking that deleting a location or a floorplan does not delete a device.


Discussion / hints:

Fix patch:
diff --git a/openwisp_controller/geo/base/models.py b/openwisp_controller/geo/base/models.py
index 81f66fbf4..519ebee5a 100644
--- a/openwisp_controller/geo/base/models.py
+++ b/openwisp_controller/geo/base/models.py
@@ -40,10 +40,10 @@ class BaseDeviceLocation(ValidateOrgMixin, AbstractObjectLocation):
     )
     # override parent foreign key targets
     location = models.ForeignKey(
-        get_model_name('geo', 'Location'), models.PROTECT, blank=True, null=True
+        get_model_name('geo', 'Location'), models.CASCADE, blank=True, null=True
     )
     floorplan = models.ForeignKey(
-        get_model_name('geo', 'FloorPlan'), models.PROTECT, blank=True, null=True
+        get_model_name('geo', 'FloorPlan'), models.CASCADE, blank=True, null=True
     )
 
     class Meta(AbstractObjectLocation.Meta):
diff --git a/openwisp_controller/geo/migrations/0003_alter_devicelocation_floorplan_location.py b/openwisp_controller/geo/migrations/0003_alter_devicelocation_floorplan_location.py
new file mode 100644
index 000000000..951c3ef95
--- /dev/null
+++ b/openwisp_controller/geo/migrations/0003_alter_devicelocation_floorplan_location.py
@@ -0,0 +1,35 @@
+# Generated by Django 3.2.16 on 2023-01-05 09:50
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('geo', '0002_default_groups_permissions'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='devicelocation',
+            name='floorplan',
+            field=models.ForeignKey(
+                blank=True,
+                null=True,
+                on_delete=django.db.models.deletion.CASCADE,
+                to=settings.GEO_FLOORPLAN_MODEL,
+            ),
+        ),
+        migrations.AlterField(
+            model_name='devicelocation',
+            name='location',
+            field=models.ForeignKey(
+                blank=True,
+                null=True,
+                on_delete=django.db.models.deletion.CASCADE,
+                to=settings.GEO_LOCATION_MODEL,
+            ),
+        ),
+    ]


Test patch:
diff --git a/tests/openwisp2/sample_geo/migrations/0003_alter_devicelocation_floorplan_location.py b/tests/openwisp2/sample_geo/migrations/0003_alter_devicelocation_floorplan_location.py
new file mode 100644
index 000000000..8e58699ba
--- /dev/null
+++ b/tests/openwisp2/sample_geo/migrations/0003_alter_devicelocation_floorplan_location.py
@@ -0,0 +1,35 @@
+# Generated by Django 3.2.16 on 2023-01-05 09:50
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('sample_geo', '0002_default_group_permissions'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='devicelocation',
+            name='floorplan',
+            field=models.ForeignKey(
+                blank=True,
+                null=True,
+                on_delete=django.db.models.deletion.CASCADE,
+                to=settings.GEO_FLOORPLAN_MODEL,
+            ),
+        ),
+        migrations.AlterField(
+            model_name='devicelocation',
+            name='location',
+            field=models.ForeignKey(
+                blank=True,
+                null=True,
+                on_delete=django.db.models.deletion.CASCADE,
+                to=settings.GEO_LOCATION_MODEL,
+            ),
+        ),
+    ]