Error while creating device: duplicate key value violates unique constraint "dcim_device_pkey" #5446

Closed
opened 2025-12-29 19:28:09 +01:00 by adam · 3 comments
Owner

Originally created by @gmiranda on GitHub (Sep 30, 2021).

NetBox version

v2.11.12

Python version

3.8

Steps to Reproduce

I first added some new devices using netbox/manage.py nbshell

from operator import itemgetter, attrgetter
from netaddr import EUI

mySite= Site.objects.filter(name="MyBuilding")[0]
location = Location.objects.filter(name="myLocation")[0]
genericDev=  DeviceType.objects.filter(model="Generic device")[0]
role = DeviceRole.objects.filter(slug="ordinadors")[0]

def create_device(deviceName, dns_name, mac, ip_address ):
	# Find the highest device id
	deviceId = 1 + sorted(Device.objects.all(),key=attrgetter('pk'), reverse=True)[0].pk
	dev = Device(id=deviceId, name=deviceName, site=mySite, location=Location.objects.filter(name="myLocation")[0], device_type= genericDev, device_role = role )
	dev.save()

Now when I try to create a new device from the web interface, any device, I get this error:

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

duplicate key value violates unique constraint "dcim_device_pkey"
DETAIL:  Key (id)=(39) already exists.


Python version: 3.8.5
NetBox version: 2.11.12

Expected Behavior

No error at all!

Observed Behavior

My guess is that I incorrectly created those devices using the netbox shell. What's wrong in those code lines?

How can I fix the primary key inconsistence?

Originally created by @gmiranda on GitHub (Sep 30, 2021). ### NetBox version v2.11.12 ### Python version 3.8 ### Steps to Reproduce I first added some new devices using netbox/manage.py nbshell ``` from operator import itemgetter, attrgetter from netaddr import EUI mySite= Site.objects.filter(name="MyBuilding")[0] location = Location.objects.filter(name="myLocation")[0] genericDev= DeviceType.objects.filter(model="Generic device")[0] role = DeviceRole.objects.filter(slug="ordinadors")[0] def create_device(deviceName, dns_name, mac, ip_address ): # Find the highest device id deviceId = 1 + sorted(Device.objects.all(),key=attrgetter('pk'), reverse=True)[0].pk dev = Device(id=deviceId, name=deviceName, site=mySite, location=Location.objects.filter(name="myLocation")[0], device_type= genericDev, device_role = role ) dev.save() ``` Now when I try to create a new device from the web interface, any device, I get this error: ``` <class 'django.db.utils.IntegrityError'> duplicate key value violates unique constraint "dcim_device_pkey" DETAIL: Key (id)=(39) already exists. Python version: 3.8.5 NetBox version: 2.11.12 ``` ### Expected Behavior No error at all! ### Observed Behavior My guess is that I incorrectly created those devices using the netbox shell. What's wrong in those code lines? How can I fix the primary key inconsistence?
adam added the type: bug label 2025-12-29 19:28:09 +01:00
adam closed this issue 2025-12-29 19:28:10 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Sep 30, 2021):

You are manually assigning the id so the id sequence doesn't get updated, you should not do that. Just remove id=deviceId.

@kkthxbye-code commented on GitHub (Sep 30, 2021): You are manually assigning the id so the id sequence doesn't get updated, you should not do that. Just remove `id=deviceId`.
Author
Owner

@gmiranda commented on GitHub (Sep 30, 2021):

Ok, I'll do. But then why the doc shows an example like this?
myvlan = VLAN(vid=123, name='MyNewVLAN', site=lab1)

Any idea on how can I fix the current DB mess?

@gmiranda commented on GitHub (Sep 30, 2021): Ok, I'll do. But then why the doc shows an example [like this?](https://netbox.readthedocs.io/en/stable/administration/netbox-shell/#creating-and-updating-objects) `myvlan = VLAN(vid=123, name='MyNewVLAN', site=lab1)` Any idea on how can I fix the current DB mess?
Author
Owner

@kkthxbye-code commented on GitHub (Sep 30, 2021):

A vid on a VLAN is not the same as the autoincrementing id that's present on all models.

You should be able to fix it by either deleting the devices you created or accessing postgres database and altering the sequence. Something like this:

ALTER SEQUENCE dcim_device_id_seq RESTART WITH 999;

Replace 999 with the id of the highest numbered device you currently have.

@kkthxbye-code commented on GitHub (Sep 30, 2021): [A vid on a VLAN](https://github.com/netbox-community/netbox/blob/develop/netbox/ipam/models/vlans.py#L116-L119) is not the same as the [autoincrementing id that's present on all models](https://github.com/netbox-community/netbox/blob/a9761e8dd21883f43e7b109c7eb845ce4f47edf9/netbox/netbox/models.py#L153-L155). You should be able to fix it by either deleting the devices you created or accessing postgres database and altering the sequence. Something like this: `ALTER SEQUENCE dcim_device_id_seq RESTART WITH 999;` Replace 999 with the id of the highest numbered device you currently have.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5446