Device primary_ip not being removed when removing address from interface #2830

Closed
opened 2025-12-29 18:22:35 +01:00 by adam · 7 comments
Owner

Originally created by @juan-vg on GitHub (Aug 28, 2019).

Environment

  • Python version: 3.5.3
  • NetBox version: 2.6.2

Steps to Reproduce

  1. Add an interface (let's call it 'I1') to a device ('D1'), and then add an IP address ('A1') to that interface 'I1'
  2. Set the tuple (Interface-I1, IP-A1) as the primary_ip for the device 'D1'
  3. Assign that IP address to a new interface ('I2') owned by a different device ('D2')
  4. Try to set that new tuple (Interface-I2, IP-A1) as the primary IP for the device 'D2'

Expected Behavior

As far as I can reassign the IP address to a new interface (which is completely logical), I should be able to set it as primary_ip on the new device.

Observed Behavior

Response from the UI (device edit): Device with this Primary IPv4 already exists.
Response from the API (PATCH /api/dcim/devices/<device-D2_id>/):

HTTP 500:

duplicate key value violates unique constraint &quot;dcim_device_primary_ip4_id_key&quot;
DETAIL:  Key (primary_ip4_id)=(<IP-A1_id>) already exists.
Originally created by @juan-vg on GitHub (Aug 28, 2019). ### Environment * Python version: 3.5.3 * NetBox version: 2.6.2 ### Steps to Reproduce 1. Add an interface (let's call it 'I1') to a device ('D1'), and then add an IP address ('A1') to that interface 'I1' 2. Set the tuple (Interface-I1, IP-A1) as the primary_ip for the device 'D1' 3. Assign that IP address to a new interface ('I2') owned by a different device ('D2') 4. Try to set that new tuple (Interface-I2, IP-A1) as the primary IP for the device 'D2' <!-- What did you expect to happen? --> ### Expected Behavior As far as I can reassign the IP address to a new interface (which is completely logical), I should be able to set it as primary_ip on the new device. <!-- What happened instead? --> ### Observed Behavior Response from the UI (device edit): Device with this Primary IPv4 already exists. Response from the API (PATCH /api/dcim/devices/<device-D2_id>/): HTTP 500: ``` duplicate key value violates unique constraint &quot;dcim_device_primary_ip4_id_key&quot; DETAIL: Key (primary_ip4_id)=(<IP-A1_id>) already exists. ```
adam added the type: bugstatus: accepted labels 2025-12-29 18:22:35 +01:00
adam closed this issue 2025-12-29 18:22:35 +01:00
Author
Owner

@jeremystretch commented on GitHub (Aug 28, 2019):

  1. Assign that IP address to a new interface ('I2') owned by a different device ('D2')

How are you making this change? Via the API?

@jeremystretch commented on GitHub (Aug 28, 2019): > 3. Assign that IP address to a new interface ('I2') owned by a different device ('D2') How are you making this change? Via the API?
Author
Owner

@juan-vg commented on GitHub (Aug 30, 2019):

Hello @jeremystretch

Yes, I'm performing that change (and the others) via the API

@juan-vg commented on GitHub (Aug 30, 2019): Hello @jeremystretch Yes, I'm performing that change (and the others) via the API
Author
Owner

@juan-vg commented on GitHub (Sep 2, 2019):

Are you telling me that this behaviour only happens when using the API? Is the UI the one triggering the release? :(

@juan-vg commented on GitHub (Sep 2, 2019): Are you telling me that this behaviour only happens when using the API? Is the UI the one triggering the release? :(
Author
Owner

@DanSheps commented on GitHub (Sep 6, 2019):

juan-vg,

You are removing the IP from the interface but otherwise leaving it in the database correct? The current setup is a foreign key on the Device table pointing to the primary ip.

Instead of re-assigning to a new device, you should delete the IP and re-create it on a new device. The UI can handle this gracefully as the UI generally requires more logic in the controllers whereas for the most part, with the API we run off of serializers so it doesn't take into account secondary events such as assigning an IP to a different device then wanting to set that IP as the primary.

If you want to do this, you should:

  1. Delete the IP
  2. Recreate it on the new device

or

  1. Unassign the IP as primary
  2. Attach it to the new device
  3. Assign it as primary

I am going to look into possible ways to clean this up however.

@DanSheps commented on GitHub (Sep 6, 2019): juan-vg, You are removing the IP from the interface but otherwise leaving it in the database correct? The current setup is a foreign key on the Device table pointing to the primary ip. Instead of re-assigning to a new device, you should delete the IP and re-create it on a new device. The UI can handle this gracefully as the UI generally requires more logic in the controllers whereas for the most part, with the API we run off of serializers so it doesn't take into account secondary events such as assigning an IP to a different device then wanting to set that IP as the primary. If you want to do this, you should: 1. Delete the IP 2. Recreate it on the new device or 1. Unassign the IP as primary 2. Attach it to the new device 3. Assign it as primary I am going to look into possible ways to clean this up however.
Author
Owner

@juan-vg commented on GitHub (Sep 12, 2019):

Hey @DanSheps! Thank you for your answer and for the workarounds :)

Sadly the workarounds are not perfectly/efficiently solving my current situation.

I have several scripts gathering real-time info from my infrastructure and then populating NetBox.

  • On the one hand I'm setting info on the IP addresses so if I delete them I will loose (meta)data.
  • On the other hand, I have a script that checks some network interfaces and updates the IP addresses they currently have (DHCP with long leases, but DHCP at all). I would need several queries to know first to who was assigned, then unassign it, and then assign it again.

I believe that this update must be done by the API because if not the DB will end up in an inconsistent state

I am going to look into possible ways to clean this up however.

I guess that a SQL trigger could be a quick solution working for the API and the UI at the same time :)

@juan-vg commented on GitHub (Sep 12, 2019): Hey @DanSheps! Thank you for your answer and for the workarounds :) Sadly the workarounds are not perfectly/efficiently solving my current situation. I have several scripts gathering real-time info from my infrastructure and then populating NetBox. - On the one hand I'm setting info on the IP addresses so if I delete them I will loose (meta)data. - On the other hand, I have a script that checks some network interfaces and updates the IP addresses they currently have (DHCP with long leases, but DHCP at all). I would need several queries to know first to who was assigned, then unassign it, and then assign it again. I believe that this update must be done by the API because if not the DB will end up in an inconsistent state > I am going to look into possible ways to clean this up however. I guess that a SQL trigger could be a quick solution working for the API and the UI at the same time :)
Author
Owner

@DanSheps commented on GitHub (Sep 12, 2019):

  • On the one hand I'm setting info on the IP addresses so if I delete them I will loose (meta)data.

It sounds like option #2 will solve this for you

  • On the other hand, I have a script that checks some network interfaces and updates the IP addresses they currently have (DHCP with long leases, but DHCP at all). I would need several queries to know first to who was assigned, then unassign it, and then assign it again.

I don't see what this has to do with either of the workarounds presented.

If you need further assistance with the workarounds specifically, I would suggest you go to the mailing list for additional support there.

@DanSheps commented on GitHub (Sep 12, 2019): > * On the one hand I'm setting info on the IP addresses so if I delete them I will loose (meta)data. It sounds like option #2 will solve this for you > * On the other hand, I have a script that checks some network interfaces and updates the IP addresses they currently have (DHCP with long leases, but DHCP at all). I would need several queries to know first to who was assigned, then unassign it, and then assign it again. I don't see what this has to do with either of the workarounds presented. If you need further assistance with the workarounds specifically, I would suggest you go to the [mailing list](https://groups.google.com/forum/#!forum/netbox-discuss) for additional support there.
Author
Owner

@jeremystretch commented on GitHub (Oct 9, 2019):

To be clear, it is intended behavior to not let an IP be moved from device A to device B while being assigned as the primary IP for device A. The parent device/VM needs to be modified to remove the assignment as primary IP before the IP can be reassigned. However, there is currently no validation in place to prevent this (via the API), so I'm calling it a bug.

@jeremystretch commented on GitHub (Oct 9, 2019): To be clear, it is intended behavior to not let an IP be moved from device A to device B while being assigned as the primary IP for device A. The parent device/VM needs to be modified to remove the assignment as primary IP before the IP can be reassigned. However, there is currently no validation in place to prevent this (via the API), so I'm calling it a bug.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2830