Unique device role #11351

Closed
opened 2025-12-29 21:44:04 +01:00 by adam · 3 comments
Owner

Originally created by @Crubumble on GitHub (Jul 8, 2025).

Originally assigned to: @jeremystretch on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.3.3

Python Version

3.10

Steps to Reproduce

  1. Add a new device role (/dcim/device-roles/add/) named "Building Automation"
  2. Add a further device role named "camera" with "Build Automation" as parent
  3. Add a further device role named "camera" with "Build Automation" as parent
  4. Add a further device role named "camera" with no parent

Expected Behavior

Creating a device role with the same hierarchy and name (and description) should not be possible.
Therefore, only the device role created under step 1,2 and 4 should be possible.

Observed Behavior

All device roles were created.
Device roles are created even if the same role exists already.

Originally created by @Crubumble on GitHub (Jul 8, 2025). Originally assigned to: @jeremystretch on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.3.3 ### Python Version 3.10 ### Steps to Reproduce 1. Add a new device role (/dcim/device-roles/add/) named "Building Automation" 2. Add a further device role named "camera" with "Build Automation" as parent 3. Add a further device role named "camera" with "Build Automation" as parent 4. Add a further device role named "camera" with no parent ### Expected Behavior Creating a device role with the same hierarchy and name (and description) should not be possible. Therefore, only the device role created under step 1,2 and 4 should be possible. ### Observed Behavior All device roles were created. Device roles are created even if the same role exists already.
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 21:44:04 +01:00
adam closed this issue 2025-12-29 21:44:04 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jul 8, 2025):

Your report above is very confusing: You appear to be conflating multiple issues in a single report.

Please revise your report above to capture a single, specific issue, and open separate bug reports for any other concerns.

@jeremystretch commented on GitHub (Jul 8, 2025): Your report above is very confusing: You appear to be conflating multiple issues in a single report. Please revise your report above to capture a single, specific issue, and open separate bug reports for any other concerns.
Author
Owner

@Crubumble commented on GitHub (Jul 9, 2025):

@jeremystretch Sorry for the confusion. I revised the post.

@Crubumble commented on GitHub (Jul 9, 2025): @jeremystretch Sorry for the confusion. I revised the post.
Author
Owner

@sleepinggenius2 commented on GitHub (Jul 9, 2025):

It looks like when DeviceRole was moved from an OrganizationalModel to a NestedGroupModel in 4.3, the previous unique constraints on name and slug were not transitioned. I believe these constraints just need to be added under DeviceRole.Meta:

constraints = (
    models.UniqueConstraint(
        fields=('parent', 'name'),
        name='%(app_label)s_%(class)s_parent_name'
    ),
    models.UniqueConstraint(
        fields=('name',),
        name='%(app_label)s_%(class)s_name',
        condition=Q(parent__isnull=True),
        violation_error_message=_("A top-level device role with this name already exists.")
    ),
    models.UniqueConstraint(
        fields=('parent', 'slug'),
        name='%(app_label)s_%(class)s_parent_slug'
    ),
    models.UniqueConstraint(
        fields=('slug',),
        name='%(app_label)s_%(class)s_slug',
        condition=Q(parent__isnull=True),
        violation_error_message=_("A top-level device role with this slug already exists.")
    ),
)
@sleepinggenius2 commented on GitHub (Jul 9, 2025): It looks like when `DeviceRole` was moved from an `OrganizationalModel` to a `NestedGroupModel` in 4.3, the previous unique constraints on `name` and `slug` were not transitioned. I believe these constraints just need to be added under `DeviceRole.Meta`: ```python constraints = ( models.UniqueConstraint( fields=('parent', 'name'), name='%(app_label)s_%(class)s_parent_name' ), models.UniqueConstraint( fields=('name',), name='%(app_label)s_%(class)s_name', condition=Q(parent__isnull=True), violation_error_message=_("A top-level device role with this name already exists.") ), models.UniqueConstraint( fields=('parent', 'slug'), name='%(app_label)s_%(class)s_parent_slug' ), models.UniqueConstraint( fields=('slug',), name='%(app_label)s_%(class)s_slug', condition=Q(parent__isnull=True), violation_error_message=_("A top-level device role with this slug already exists.") ), ) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11351