Group models with null parents allow duplicate names #5720

Closed
opened 2025-12-29 19:31:50 +01:00 by adam · 3 comments
Owner

Originally created by @DanSheps on GitHub (Dec 1, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

3.1-beta

Python version

3.8

Steps to Reproduce

  1. Create Region named Test
  2. Create Region named Test

Expected Behavior

The second region creation should fail

Observed Behavior

The creation is allowed

Originally created by @DanSheps on GitHub (Dec 1, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version 3.1-beta ### Python version 3.8 ### Steps to Reproduce 1. Create Region named Test 2. Create Region named Test ### Expected Behavior The second region creation should fail ### Observed Behavior The creation is allowed
adam added the type: bugstatus: acceptedbeta labels 2025-12-29 19:31:50 +01:00
adam closed this issue 2025-12-29 19:31:50 +01:00
Author
Owner

@DanSheps commented on GitHub (Dec 1, 2021):

With a unique constraint of "unique_together" with (parent, name), this will allow duplicate named due to the way Postgres handles null values (treats each null value as unique).

Two proposals were brought up to handle this previously:

  1. A unqiue index which coaleses the parent and the name together, resulting in a string that cannot be unique
  2. Solve this in code
@DanSheps commented on GitHub (Dec 1, 2021): With a unique constraint of "unique_together" with (parent, name), this will allow duplicate named due to the way Postgres handles null values (treats each null value as unique). Two proposals were brought up to handle this previously: 1. A unqiue index which coaleses the parent and the name together, resulting in a string that cannot be unique 2. Solve this in code
Author
Owner

@jeremystretch commented on GitHub (Dec 3, 2021):

The same issue exists for site groups and locations as well.

@jeremystretch commented on GitHub (Dec 3, 2021): The same issue exists for site groups and locations as well.
Author
Owner

@jeremystretch commented on GitHub (Dec 3, 2021):

I looked into employing Django's unique constraints for this. While this approach works for effecting database-level validation, violations unfortunately result in an unhandled IntegrityError exception: There is no pre-save validation logic. AFAICT we still need to define a custom validate_unique() method on these models to handle the pre-save validation cleanly and prevent the exception.

@jeremystretch commented on GitHub (Dec 3, 2021): I looked into employing Django's [unique constraints](https://docs.djangoproject.com/en/3.2/ref/models/constraints/#uniqueconstraint) for this. While this approach works for effecting database-level validation, violations unfortunately result in an unhandled IntegrityError exception: There is no pre-save validation logic. AFAICT we still need to define a custom `validate_unique()` method on these models to handle the pre-save validation cleanly and prevent the exception.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5720