Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions netbox/core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from core.choices import JobStatusChoices, ObjectChangeActionChoices
from core.events import *
from core.models import ObjectType
from utilities.object_types import objecttype_table_exists
from extras.events import enqueue_event
from extras.models import Tag
from extras.utils import run_validators
Expand Down Expand Up @@ -51,6 +52,10 @@ def update_object_types(sender, **kwargs):
"""
Create or update the corresponding ObjectType for each model within the migrated app.
"""
# Skip ObjectType operations if the table doesn't exist yet (during migrations)
if not objecttype_table_exists():
return

for model in sender.get_models():
app_label, model_name = model._meta.label_lower.split('.')

Expand Down
5 changes: 5 additions & 0 deletions netbox/netbox/models/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from netbox.signals import post_clean
from netbox.utils import register_model_feature
from utilities.json import CustomFieldJSONEncoder
from utilities.object_types import objecttype_table_exists
from utilities.serialization import serialize_object

__all__ = (
Expand Down Expand Up @@ -670,6 +671,10 @@ def has_feature(model_or_ct, feature):
"""
Returns True if the model supports the specified feature.
"""
# Check if ObjectType table exists before attempting queries
if not objecttype_table_exists():
return False

# If an ObjectType was passed, we can use it directly
if type(model_or_ct) is ObjectType:
ot = model_or_ct
Expand Down
5 changes: 5 additions & 0 deletions netbox/netbox/search/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from core.models import ObjectType
from extras.models import CachedValue, CustomField
from netbox.registry import registry
from utilities.object_types import objecttype_table_exists
from utilities.object_types import object_type_identifier
from utilities.querysets import RestrictedPrefetch
from utilities.string import title
Expand Down Expand Up @@ -209,6 +210,10 @@ def cache(self, instances, indexer=None, remove_existing=True):
break

# Prefetch any associated custom fields
# Skip if ObjectType table doesn't exist yet (during migrations)
if not objecttype_table_exists():
return

object_type = ObjectType.objects.get_for_model(indexer.model)
custom_fields = CustomField.objects.filter(object_types=object_type).exclude(search_weight=0)

Expand Down
13 changes: 13 additions & 0 deletions netbox/utilities/object_types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.db import connection

from .string import title

__all__ = (
'object_type_identifier',
'object_type_name',
'objecttype_table_exists',
)


Expand All @@ -27,3 +30,13 @@ def object_type_name(object_type, include_app=True):
except AttributeError:
# Model does not exist
return f'{object_type.app_label} > {object_type.model}'


def objecttype_table_exists():
"""
Check if the core_objecttype table exists.

Returns True if the table exists, False otherwise.
Used to prevent ObjectType queries during migrations when the table doesn't exist yet.
"""
return 'core_objecttype' in connection.introspection.table_names()