Adding a Uniqueness Rule to Existing Data
Before enforcing uniqueness, examine which current rows would share a key and decide which changes preserve the domain's relationships.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace two repeated codes
Two catalogue records assigned code A could be separate entities or unintended duplicates. Compare their identity and related rows before correcting the code. See entity identity.
Retain a repeatable procedure
Verify old records, update relationships consistently where needed, and confirm the new constraint accepts intended writes. Uniqueness constraints define the resulting policy.[1][2]
Find real duplicate groups before enforcing the constraint
Suppose a service wants codes unique within each project. Group historical rows by project and code and inspect groups with count above one. Decide how to preserve references during resolution. Compare with the database's intended equality and collation. See column uniqueness.
(1,red) occurs twice → conflict
(2,red) occurs once → independent
Resolve or explicitly preserve both before imposing the rule
Account for writes between cleanup and enforcement
An old writer could recreate a duplicate after cleanup. Coordinate migration and application write policy so the final constraint can be applied successfully. Test representative duplicates and confirm the invariant afterward.