Skip to content

Conversation

arthanson
Copy link
Contributor

Raises a validation error if trying to set unique on a field and non-unique data exists. The other way to do this is to query all the data in the table and annotate with a count of occurrences but that could be fairly DB heavy, this way just tries to create the index we will be creating later in save.

The code to do that is a bit different as you need to throw a unique error so that it will roll-back the transaction as you don't want to actually do the alter here, so the first try-catch is to catch the unique case to roll back the transaction and swallow the error, the try-catch on the IntegrityError is the one that raises the actual ValidationError.

Monosnap Editing custom object type field f1 | NetBox 2025-08-19 13-45-06

@arthanson arthanson marked this pull request as ready for review August 19, 2025 21:00
@arthanson arthanson requested a review from bctiemann August 19, 2025 21:00
@bctiemann bctiemann merged commit 962de13 into main Aug 20, 2025
11 checks passed
@bctiemann bctiemann deleted the npl-389-unique branch August 20, 2025 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants