validate_unique() is not enforced during API validation #3456

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

Originally created by @lampwins on GitHub (Mar 10, 2020).

Environment

  • Python version: 3.7
  • NetBox version: 2.7.10

Steps to Reproduce

  1. Create a virtualization cluster
  2. In the API, create a VM with the name "vm1" and assigned to the cluster in step 1
  3. In the API, create a second VM with the same name "vm1" and assigned to the same cluster from step 1

Expected Behavior

The unique constraint placed on VM name, cluster, and tenant should prevent the second VM from being created. This is true in the web UI.

Observed Behavior

The API validation ignores this unique constraint because of our use of validate_unique() on the virtual machine model. Django's ModelForm class does account for calling validate_unique() but we have implemented our own model validation in the API by way of ValidatedModelSerializer and it does not make a call to validate_unique(). We just need to call instance.validate_unique() after instance.clean()

Originally created by @lampwins on GitHub (Mar 10, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.7 * NetBox version: 2.7.10 <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox (or the current beta release where applicable). Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a wrapper like pynetbox. --> ### Steps to Reproduce 1. Create a virtualization cluster 2. In the API, create a VM with the name "vm1" and assigned to the cluster in step 1 3. In the API, create a second VM with the same name "vm1" and assigned to the same cluster from step 1 <!-- What did you expect to happen? --> ### Expected Behavior The unique constraint placed on VM name, cluster, and tenant should prevent the second VM from being created. This is true in the web UI. <!-- What happened instead? --> ### Observed Behavior The API validation ignores this unique constraint because of our use of `validate_unique()` on the virtual machine model. Django's ModelForm class does account for calling `validate_unique()` but we have implemented our own model validation in the API by way of `ValidatedModelSerializer` and it does not make a call to `validate_unique()`. We just need to call `instance.validate_unique()` after `instance.clean()`
adam added the type: bugstatus: accepted labels 2025-12-29 18:29:18 +01:00
adam closed this issue 2025-12-29 18:29:18 +01:00
Author
Owner

@lampwins commented on GitHub (Mar 10, 2020):

To clarify, the reason the unique constraint is "ignored" is because in NULL != NULL in unique comparisons which is why we have to implement validate_unique() in the first place to account for the possible null tenant field.

@lampwins commented on GitHub (Mar 10, 2020): To clarify, the reason the unique constraint is "ignored" is because in NULL != NULL in unique comparisons which is why we have to implement `validate_unique()` in the first place to account for the possible null tenant field.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3456