Bug in validate uniqueness of VM name #4318

Closed
opened 2025-12-29 18:34:43 +01:00 by adam · 1 comment
Owner

Originally created by @xcdr on GitHub (Dec 1, 2020).

Environment

  • Python version: 3.8.6
  • NetBox version: 2.9.10

Cannot add VM without Tenant to different clusters.

Steps to Reproduce

  1. Add cluster A, B
  2. Add VM named "Example VM" to cluster A without Tenant
  3. Try to add VM named "Example VM" to cluster B without Tenant

Expected Behavior

VM should be added.

Observed Behavior

I see validation error: A virtual machine with this name already exists.

I see that validate_unique(self, exclude=None) in VirtualMachine model doesn't filter by cluster!

Originally created by @xcdr on GitHub (Dec 1, 2020). ### Environment * Python version: 3.8.6 * NetBox version: 2.9.10 Cannot add VM without Tenant to different clusters. ### Steps to Reproduce 1. Add cluster A, B 2. Add VM named "Example VM" to cluster A without Tenant 3. Try to add VM named "Example VM" to cluster B without Tenant ### Expected Behavior VM should be added. ### Observed Behavior I see validation error: A virtual machine with this name already exists. I see that `validate_unique(self, exclude=None)` in VirtualMachine model doesn't filter by cluster!
adam added the type: bugstatus: accepted labels 2025-12-29 18:34:43 +01:00
adam closed this issue 2025-12-29 18:34:43 +01:00
Author
Owner

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

The ability to have multiple VirtualMachine instances with duplicate names was introduced in #2669, however looking back it's not entirely clear what the constraints were intended to be. The current code looks like this:

def validate_unique(self, exclude=None):
    if self.tenant is None and VirtualMachine.objects.exclude(pk=self.pk).filter(
        name=self.name, tenant__isnull=True
    ):

This allows duplicate names only for VMs which do not have a tenant assigned, but makes no reference to Cluster. Meta.unique_together sets ['cluster', 'tenant', 'name'], which leads me to believe the intent was to constrain scope to individual cluster-tenant pairs.

We can "fix" this by extending validate_unique() to check against assigned cluster as well, which I think makes sense.

@jeremystretch commented on GitHub (Dec 3, 2020): The ability to have multiple VirtualMachine instances with duplicate names was introduced in #2669, however looking back it's not entirely clear what the constraints were intended to be. The current code looks like this: ```python def validate_unique(self, exclude=None): if self.tenant is None and VirtualMachine.objects.exclude(pk=self.pk).filter( name=self.name, tenant__isnull=True ): ``` This allows duplicate names only for VMs which do not have a tenant assigned, but makes no reference to Cluster. `Meta.unique_together` sets `['cluster', 'tenant', 'name']`, which leads me to believe the intent was to constrain scope to individual cluster-tenant pairs. We can "fix" this by extending `validate_unique()` to check against assigned cluster as well, which I think makes sense.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4318