netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.12k stars 2.58k forks source link

Invalid GraphQL output after deleting object used for object-type Custom Field #12635

Open mikkelmk opened 1 year ago

mikkelmk commented 1 year ago

NetBox version

v3.5.1

Python version

3.10

Steps to Reproduce

  1. Create object-type custom field with Interface as content type and Tag as object type
  2. Assign a tag to this custom field on an interface
  3. Delete the tag
  4. Querying the custom fields on the interface through GraphQL will still show the field as being set to the id of the deleted tag

Expected Behavior

GraphQL output like this:

{
  "data": {
    "interface": {
      "custom_fields": {
        "FooCustomField": null
      }
    }
  }
}

Observed Behavior

GraphQL output like this:

{
  "data": {
    "interface": {
      "custom_fields": {
        "FooCustomField": 42
      }
    }
  }
}

Note that the field will be null in both the Web interface and REST API - I believe that what happens is that the underlying custom_field_data on the interface is not cleared, but that the Web/API layer knows to nullify the invalid reference

arthanson commented 1 year ago

@jeremystretch I don't think there is a way to do this performantly with the current design, since it is a content_type the reference to the object is stored in the json field, so there isn't a DB reference to signal the deletion of the referenced object. When deserializing from GraphQL it just returns the value of the json, to do this would need to query the referenced object to make sure it exists which would work for a single query but not a list (n+1 queries).

We could make a GFK mapping table for custom field object references (GFK -> GFK) That could clean up when the referenced object is deleted, but I don't think we would want to do that just for this.

jeremystretch commented 1 year ago

One idea I've had on the back burner is reimplementing the custom fields feature in NetBox to utilize discrete database fields (as opposed to monolithic JSON blobs) managed via dynamic schema migrations. I haven't gotten around to drafting an FR for that yet, but it occurs to me that such a change would resolve this bug as well.

github-actions[bot] commented 7 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

jeremystretch commented 5 months ago

@arthanson could you revisit this now that we've switched from Graphene to Strawberry in NetBox v4.0?

arthanson commented 5 months ago

This isn't blocked on GraphQL, it is blocked on Custom Field data being JSON blobs which would make this non-performant.

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.