Labels: bug, data-model, admin
Linked Sentry: SENSORSAFRICA-API-140 — https://code-for-africa.sentry.io/issues/7570022076/
Environment: production
Summary
Deleting a SensorLocation in admin fails with:
update or delete on table "sensors_sensorlocation" violates foreign key constraint "fk_sensorlocation" on table "sensors_dailyavg2023"
Root cause
sensors_dailyavg2023 has a Postgres-level FK to sensors_sensorlocation that is not declared as a ForeignKey field in the Django model. Django's deletion collector doesn't know the relationship exists, so it doesn't cascade/null it before issuing the DELETE — Postgres correctly rejects it.
Fix
Add the missing ForeignKey field to the Django model with an explicit on_delete (needs a product decision: CASCADE vs PROTECT vs SET_NULL — does deleting a location wipe historical daily averages, or should locations with historical data be undeletable?).
To check
Whether dailyavg2024/dailyavg2025 have the same gap. if so, fix should apply to all year-tables, not just 2023.
Labels: bug, data-model, admin
Linked Sentry: SENSORSAFRICA-API-140 — https://code-for-africa.sentry.io/issues/7570022076/
Environment: production
Summary
Deleting a SensorLocation in admin fails with:
update or delete on table "sensors_sensorlocation" violates foreign key constraint "fk_sensorlocation" on table "sensors_dailyavg2023"
Root cause
sensors_dailyavg2023has a Postgres-level FK tosensors_sensorlocationthat is not declared as aForeignKeyfield in the Django model. Django's deletion collector doesn't know the relationship exists, so it doesn't cascade/null it before issuing the DELETE — Postgres correctly rejects it.Fix
Add the missing
ForeignKeyfield to the Django model with an expliciton_delete(needs a product decision: CASCADE vs PROTECT vs SET_NULL — does deleting a location wipe historical daily averages, or should locations with historical data be undeletable?).To check
Whether
dailyavg2024/dailyavg2025have the same gap. if so, fix should apply to all year-tables, not just 2023.