[bug] Applying 0008_alter_deviceconnection_unique_together fails if database has duplicate objects
pandafy opened this issue · 1 comments
In PR #806, a unique_together constraint was added to the DeviceConnection model on device_id and credentials_id fields. However, applying the migration may fail if the database already contains objects that contradict this unique constraint. To address this, we need to implement a function to remove the conflicting objects from the database before applying the migration.
Proposed Solution
Implement a pre-migration function that identifies and removes objects conflicting with the unique constraint.
Ensure that this function is executed before applying the openwisp_controller/connection/migrations/0008_alter_deviceconnection_unique_together.py
migration to prevent migration failures.
I used this code to find the objects that were failing the unique together constraint
DeviceConnection.objects.values('device_id', 'credentials_id').annotate(count=models.Count('id')).filter(count__gt=1)