Description
NetBox version
v3.6.3
Feature type
Change to existing functionality
Proposed functionality
Synopsis
Custom field data is currently stored in a single custom_field_data
JSON column on each PotsgreSQL table for models which support custom field assignment. Within this database field, custom field data exists as a mapping of field name to value. For example:
Table "public.dcim_site"
Column | Type | Collation | Nullable | Default
-------------------+--------------------------+-----------+----------+----------------------------------
created | timestamp with time zone | | |
last_updated | timestamp with time zone | | |
custom_field_data | jsonb | | not null |
...
>>> site.custom_field_data
{'locode': 'AE-AMU', 'zip_code': '12345'}
This FR proposes rearchitecting the current implementation so that each custom field exists as a discrete table column within the database. In the example above, locode
and zip_code
would each exist as individual columns on the dcim_site
table.
Adding and Removing Custom Fields
Creating and deleting custom fields on models would trigger the creation/deletion of the corresponding table column for the affected models. The same utilities employed by Django to effect schema migrations in response to model changes could theoretically be employed for this purpose. We could also implement validation that checks for existing data in any particular column prior to its deletion.
Table columns associated with custom fields would not be managed by native Django migrations, and would have no impact on NetBox's native schema migrations. Similarly, this change would not impact the REST or GraphQL APIs, which would continue to represent custom field data in the current format.
Use case
Assuming a robust implementation can be identified, this change would introduce several substantial benefits:
- Greatly simplified filtering and ordering of objects by custom field values
- Improved support for custom field validation
- The ability to optimize the population of related objects (see Improve performance of object type custom field lookups for API responses #12917)
Database changes
This change would replace the existing custom_field_data
column on various models with zero or more discrete columns, one for each associated custom field. Below is a (hastily) proposed mapping of custom field types to PostgreSQL data types.
Custom Field | Column Type |
---|---|
Text | text |
Long text | text |
Integer | bigint |
Decimal | numeric |
Boolean | boolean |
Date | date |
Date & time | timestamp |
URL | char |
JSON | json |
Select | char |
Multi-select | array[char] |
Object | bigint* |
Multi-obejct | array[bigint]* |
Object and potentially multi-object fields should be treated as foreign keys; further research is needed in this area.
External dependencies
None have been identified at this time, although it's likely we'll lean on Django's built-in migration utilities for this work.