assigned_object_type may be set even when assigned_object is null #7697

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

Originally created by @candlerb on GitHub (Mar 1, 2023).

NetBox version

v3.4.5

Python version

3.8

Steps to Reproduce

Seen on current system:

# curl -sS -H "Authorization: Token $TOKEN" "$NETBOX/api/ipam/ip-addresses/?assigned_to_interface=false" | jq . | egrep 'assigned_object_(type|id)'
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "ipam.fhrpgroup",
      "assigned_object_id": 5,
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "ipam.fhrpgroup",
      "assigned_object_id": 4,
      "assigned_object_type": null,
      "assigned_object_id": null,
      "assigned_object_type": null,
      "assigned_object_id": null,
      "assigned_object_type": null,
      "assigned_object_id": null,
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "dcim.interface",
      "assigned_object_id": null,
      "assigned_object_type": "ipam.fhrpgroup",
      "assigned_object_id": 4,

Note that there are 7 IP addresses assigned with "assigned_object_type" = "dcim.interface", even though there is no assigned object.

Using a database query instead for the same result:

netbox3=# select id,assigned_object_type_id,assigned_object_id from ipam_ipaddress where assigned_object_type_id is not null and assigned_object_id is null;
 id  | assigned_object_type_id | assigned_object_id
-----+-------------------------+--------------------
 201 |                      17 |
  68 |                      17 |
 119 |                      17 |
 120 |                      17 |
 122 |                      17 |
 200 |                      17 |
 117 |                      17 |
(7 rows)

Expected Behavior

I would expect that whenever assigned_object_id is null, that assigned_object_type would be null too.

Observed Behavior

As shown above, some IPs in database have assigned_object_type present even when assigned_object_id is null.

I think there are two parts to this:

  1. Determining what bit of model validation is missing, that allowed the data to get into this state
  2. Running a migration to clean up bad data

Possibly related: #10221, #11454.

A similar issue was reported in #11726 but the opposite way round (assigned_object_id was not null, but assigned_object_type was null)

Originally created by @candlerb on GitHub (Mar 1, 2023). ### NetBox version v3.4.5 ### Python version 3.8 ### Steps to Reproduce Seen on current system: ``` # curl -sS -H "Authorization: Token $TOKEN" "$NETBOX/api/ipam/ip-addresses/?assigned_to_interface=false" | jq . | egrep 'assigned_object_(type|id)' "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "ipam.fhrpgroup", "assigned_object_id": 5, "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "ipam.fhrpgroup", "assigned_object_id": 4, "assigned_object_type": null, "assigned_object_id": null, "assigned_object_type": null, "assigned_object_id": null, "assigned_object_type": null, "assigned_object_id": null, "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "dcim.interface", "assigned_object_id": null, "assigned_object_type": "ipam.fhrpgroup", "assigned_object_id": 4, ``` Note that there are 7 IP addresses assigned with "assigned_object_type" = "dcim.interface", even though there is no assigned object. Using a database query instead for the same result: ``` netbox3=# select id,assigned_object_type_id,assigned_object_id from ipam_ipaddress where assigned_object_type_id is not null and assigned_object_id is null; id | assigned_object_type_id | assigned_object_id -----+-------------------------+-------------------- 201 | 17 | 68 | 17 | 119 | 17 | 120 | 17 | 122 | 17 | 200 | 17 | 117 | 17 | (7 rows) ``` ### Expected Behavior I would expect that whenever assigned_object_id is null, that assigned_object_type would be null too. ### Observed Behavior As shown above, some IPs in database have assigned_object_type present even when assigned_object_id is null. I think there are two parts to this: 1. Determining what bit of model validation is missing, that allowed the data to get into this state 2. Running a migration to clean up bad data Possibly related: #10221, #11454. A similar issue was reported in #11726 but the opposite way round (assigned_object_id was not null, but assigned_object_type was null)
adam added the type: bugstatus: duplicate labels 2025-12-29 20:27:11 +01:00
adam closed this issue 2025-12-29 20:27:12 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 1, 2023):

This is covered by #10221; please see my comment here. Django does not validate GenericForeignKeys in this manner natively, so potential solutions are being evaluated.

@jeremystretch commented on GitHub (Mar 1, 2023): This is covered by #10221; please see my comment [here](https://github.com/netbox-community/netbox/issues/10221#issuecomment-1234192771). Django does not validate GenericForeignKeys in this manner natively, so potential solutions are being evaluated.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7697