Skip to content

Add unique constraints instead of implicitly relying on uniqueness

Paweł Wieczorek requested to merge add-unique-constraints into collabora/staging

Every unique constraint is also an index. Avoid duplicated indexes. Replace any unique_together with a UniqueConstraint as recommended by Django documentation.

Some databases might already contain uniqueness violations which will prevent the migration from completing. One of the solutions is removing the offending entities.

For example, if there is TestJob that has multiple TestCases with "job" names. Following code can remove all TestJobs with duplicates:

q = TestJob.objects.annotate(job_cases=models.Count("pk", filter=models.Q(testsuite__testcase__name="job"))).filter(job_cases__gt=1)
q.delete()

This patch is a part of upstream MR: https://gitlab.com/lava/lava/-/merge_requests/2235

Blocked by: !165 (merged) (and follow-up action on the instance)

Merge request reports