Duplicated contact assignment error on upgrade to 3.4-beta1 with different priorities assigned #7247

Closed
opened 2025-12-29 20:20:51 +01:00 by adam · 4 comments
Owner

Originally created by @zmaster7 on GitHub (Nov 17, 2022).

Originally assigned to: @arthanson on GitHub.

NetBox version

v3.3.7

Python version

3.8

Steps to Reproduce

  1. Create contact role "Technical Support"
  2. Create contact "Foo"
  3. Create VM "Bar"
  4. Assign contact ("Foo") to the VM ("Bar") under role "Technical Support" with priority "Primary"
  5. Assign contact ("Foo") to the VM ("Bar") under role "Technical Support" with priority "Secondary"
  6. Attempt to upgrade to v3.4-beta1 with upgrade.sh

Expected Behavior

The upgrade completes without error

Observed Behavior

Receive error that the contact assignment is duplicated.

Example:
psycopg2.errors.UniqueViolation: could not create unique index "tenancy_contactassignment_unique_object_contact_role"
DETAIL: Key (content_type_id, object_id, contact_id, role_id)=(67, 1226, 26, 1) is duplicated.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "netbox/manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/init.py", line 446, in execute_from_command_line
utility.execute()
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/init.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/base.py", line 96, in wrapped
res = handle_func(*args, **kwargs)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 349, in handle
post_migrate_state = executor.migrate(
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 135, in migrate
state = self._migrate_all_forwards(
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
state = self.apply_migration(
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
state = migration.apply(state, schema_editor)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 130, in apply
operation.database_forwards(
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/operations/models.py", line 1040, in database_forwards
schema_editor.add_constraint(model, self.constraint)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 515, in add_constraint
self.execute(sql, params=None)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 199, in execute
cursor.execute(sql, params)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 87, in _execute
return self.cursor.execute(sql)
File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django_prometheus/db/common.py", line 71, in execute
return super().execute(*args, **kwargs)
django.db.utils.IntegrityError: could not create unique index "tenancy_contactassignment_unique_object_contact_role"
DETAIL: Key (content_type_id, object_id, contact_id, role_id)=(67, 1226, 26, 1) is duplicated.

Originally created by @zmaster7 on GitHub (Nov 17, 2022). Originally assigned to: @arthanson on GitHub. ### NetBox version v3.3.7 ### Python version 3.8 ### Steps to Reproduce 1. Create contact role "Technical Support" 2. Create contact "Foo" 3. Create VM "Bar" 5. Assign contact ("Foo") to the VM ("Bar") under role "Technical Support" with priority "Primary" 6. Assign contact ("Foo") to the VM ("Bar") under role "Technical Support" with priority "Secondary" 7. Attempt to upgrade to v3.4-beta1 with upgrade.sh ### Expected Behavior The upgrade completes without error ### Observed Behavior Receive error that the contact assignment is duplicated. Example: psycopg2.errors.UniqueViolation: could not create unique index "tenancy_contactassignment_unique_object_contact_role" DETAIL: Key (content_type_id, object_id, contact_id, role_id)=(67, 1226, 26, 1) is duplicated. The above exception was the direct cause of the following exception: Traceback (most recent call last): File "netbox/manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/base.py", line 96, in wrapped res = handle_func(*args, **kwargs) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 349, in handle post_migrate_state = executor.migrate( File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 135, in migrate state = self._migrate_all_forwards( File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards state = self.apply_migration( File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 252, in apply_migration state = migration.apply(state, schema_editor) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 130, in apply operation.database_forwards( File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/migrations/operations/models.py", line 1040, in database_forwards schema_editor.add_constraint(model, self.constraint) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 515, in add_constraint self.execute(sql, params=None) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 199, in execute cursor.execute(sql, params) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers( File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers return executor(sql, params, many, context) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 87, in _execute return self.cursor.execute(sql) File "/opt/netbox-3.4-beta1/venv/lib/python3.8/site-packages/django_prometheus/db/common.py", line 71, in execute return super().execute(*args, **kwargs) django.db.utils.IntegrityError: could not create unique index "tenancy_contactassignment_unique_object_contact_role" DETAIL: Key (content_type_id, object_id, contact_id, role_id)=(67, 1226, 26, 1) is duplicated.
adam added the type: bugstatus: under review labels 2025-12-29 20:20:51 +01:00
adam closed this issue 2025-12-29 20:20:51 +01:00
Author
Owner

@jeremystretch commented on GitHub (Nov 17, 2022):

This appears to be a bug in v3.3. The uniqueness constraint for ContactAssignment is:

unique_together = ('content_type', 'object_id', 'contact', 'role', 'priority')

It makes no sense to permit the assignment of a contact to the same object and role with different priorities.

@jeremystretch commented on GitHub (Nov 17, 2022): This appears to be a bug in v3.3. The uniqueness constraint for ContactAssignment is: ``` unique_together = ('content_type', 'object_id', 'contact', 'role', 'priority') ``` It makes no sense to permit the assignment of a contact to the same object and role with different priorities.
Author
Owner

@zmaster7 commented on GitHub (Nov 17, 2022):

This appears to be a bug in v3.3. The uniqueness constraint for ContactAssignment is:

unique_together = ('content_type', 'object_id', 'contact', 'role', 'priority')

It makes no sense to permit the assignment of a contact to the same object and role with different priorities.

I agree that it doesn't make sense to assign the same contact to both priorities. In my case someone had left the org and their assignments were programmatically reassigned to another person, which resulted in one instance having the same contact applied for both priorities.

With that said, I wasn't 100% sure which version had the intended unique constraint for ContactAssignment.

@zmaster7 commented on GitHub (Nov 17, 2022): > This appears to be a bug in v3.3. The uniqueness constraint for ContactAssignment is: > > ``` > unique_together = ('content_type', 'object_id', 'contact', 'role', 'priority') > ``` > > It makes no sense to permit the assignment of a contact to the same object and role with different priorities. I agree that it doesn't make sense to assign the same contact to both priorities. In my case someone had left the org and their assignments were programmatically reassigned to another person, which resulted in one instance having the same contact applied for both priorities. With that said, I wasn't 100% sure which version had the intended unique constraint for ContactAssignment.
Author
Owner

@arthanson commented on GitHub (Nov 30, 2022):

@jeremystretch closing this as it is fixed in 3.4 and unique_together is changed to UniqueConstraint in 3.4 so would require a new migration in 3.3 and then make a new migration for 3.4. Code in 3.4 has priority removed:

       constraints = (
            models.UniqueConstraint(
                fields=('content_type', 'object_id', 'contact', 'role'),
                name='%(app_label)s_%(class)s_unique_object_contact_role'
            ),
        )

@arthanson commented on GitHub (Nov 30, 2022): @jeremystretch closing this as it is fixed in 3.4 and unique_together is changed to UniqueConstraint in 3.4 so would require a new migration in 3.3 and then make a new migration for 3.4. Code in 3.4 has priority removed: ``` constraints = ( models.UniqueConstraint( fields=('content_type', 'object_id', 'contact', 'role'), name='%(app_label)s_%(class)s_unique_object_contact_role' ), ) ```
Author
Owner

@sincomil commented on GitHub (Feb 17, 2023):

Migrating from 3.3.10 to 3.4.4 and got similar error:

Running migrations:
  Applying tenancy.0008_unique_constraints...Traceback (most recent call last):
  File "/opt/netbox-git/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.UniqueViolation: could not create unique index "tenancy_contactassignment_unique_object_contact_role"
DETAIL: Key (content_type_id, object_id, contact_id, role_id)=(87, 1499, 4, 2) is duplicated.
@sincomil commented on GitHub (Feb 17, 2023): Migrating from 3.3.10 to 3.4.4 and got similar error: ``` Running migrations: Applying tenancy.0008_unique_constraints...Traceback (most recent call last): File "/opt/netbox-git/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute return self.cursor.execute(sql) psycopg2.errors.UniqueViolation: could not create unique index "tenancy_contactassignment_unique_object_contact_role" DETAIL: Key (content_type_id, object_id, contact_id, role_id)=(87, 1499, 4, 2) is duplicated. ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7247