Exception when adding the same contact multiple times #7002

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

Originally created by @candlerb on GitHub (Sep 19, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.3.4

Python version

3.8

Steps to Reproduce

  1. Create any contact, say "Contact1", and one Contact role, say "Admin"
  2. Go to any Location
  3. Under the Contacts panel, click "Add a contact"
  4. Select Contact1 and Admin
  5. Click Create and Add Another
  6. Select Contact1 and Admin again
  7. Click Create

Expected Behavior

A form validation message saying that the contact already exists.

Optionally: existing contacts being disabled or hidden in the drop-down menu of available contacts.

Observed Behavior

An exception was shown in the UI.

<class 'django.db.utils.IntegrityError'>

duplicate key value violates unique constraint "tenancy_contactassignmen_content_type_id_object_i_0764a1ef_uniq"
DETAIL:  Key (content_type_id, object_id, contact_id, role_id, priority)=(34, 1, 1, 1, ) already exists.

Python version: 3.8.10
NetBox version: 3.3.4
Originally created by @candlerb on GitHub (Sep 19, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.3.4 ### Python version 3.8 ### Steps to Reproduce 1. Create any contact, say "Contact1", and one Contact role, say "Admin" 2. Go to any Location 3. Under the Contacts panel, click "Add a contact" 4. Select Contact1 and Admin 5. Click Create and Add Another 6. Select Contact1 and Admin again 7. Click Create ### Expected Behavior A form validation message saying that the contact already exists. Optionally: existing contacts being disabled or hidden in the drop-down menu of available contacts. ### Observed Behavior An exception was shown in the UI. ``` <class 'django.db.utils.IntegrityError'> duplicate key value violates unique constraint "tenancy_contactassignmen_content_type_id_object_i_0764a1ef_uniq" DETAIL: Key (content_type_id, object_id, contact_id, role_id, priority)=(34, 1, 1, 1, ) already exists. Python version: 3.8.10 NetBox version: 3.3.4 ```
adam added the type: bugstatus: accepted labels 2025-12-29 19:47:40 +01:00
adam closed this issue 2025-12-29 19:47:40 +01:00
Author
Owner

@jeremystretch commented on GitHub (Sep 27, 2022):

I see the issue. Normally, fields defined under the model's Meta.unique_together are evaluated for uniqueness, and a ValidationError is raised for any violation. However, because ContactAssignment's content_type and object_id fields are omitted from the model form (because the object is assigned within the view), this validation is being bypassed.

We should be able to resolve this by adding content_type and object_id as hidden fields on ContactAssignmentForm.

@jeremystretch commented on GitHub (Sep 27, 2022): I see the issue. Normally, fields defined under the model's `Meta.unique_together` are evaluated for uniqueness, and a ValidationError is raised for any violation. However, because ContactAssignment's `content_type` and `object_id` fields are omitted from the model form (because the object is assigned within the view), this validation is being bypassed. We should be able to resolve this by adding `content_type` and `object_id` as hidden fields on ContactAssignmentForm.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7002