From b2f12ac84a86665d2af86d1544ec8640c13ba952 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 19 Sep 2025 10:03:34 -0700 Subject: [PATCH 1/2] avoid postgres errors pre migration run --- netbox_custom_objects/__init__.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/netbox_custom_objects/__init__.py b/netbox_custom_objects/__init__.py index 4d571bb..e8249c1 100644 --- a/netbox_custom_objects/__init__.py +++ b/netbox_custom_objects/__init__.py @@ -24,18 +24,23 @@ def check_custom_object_type_table_exists(): Check if the CustomObjectType table exists in the database. Returns True if the table exists, False otherwise. """ + from django.db import connection from .models import CustomObjectType try: - # Try to query the model - if the table doesn't exist, this will raise an exception - # this check and the transaction.atomic() is only required when running tests as the - # migration check doesn't work correctly in the test environment - with transaction.atomic(): - # Force immediate execution by using first() - CustomObjectType.objects.first() - return True + # Use raw SQL to check table existence without generating ORM errors + with connection.cursor() as cursor: + table_name = CustomObjectType._meta.db_table + cursor.execute(""" + SELECT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = %s + ) + """, [table_name]) + table_exists = cursor.fetchone()[0] + return table_exists except (OperationalError, ProgrammingError, DatabaseError): - # Catch database-specific errors (table doesn't exist, permission issues, etc.) + # Catch database-specific errors (permission issues, etc.) return False From ecf7a83c202f3fa46c8cee0009de2126b42d84d8 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 19 Sep 2025 10:06:24 -0700 Subject: [PATCH 2/2] avoid postgres errors pre migration run --- netbox_custom_objects/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/netbox_custom_objects/__init__.py b/netbox_custom_objects/__init__.py index e8249c1..f8b948e 100644 --- a/netbox_custom_objects/__init__.py +++ b/netbox_custom_objects/__init__.py @@ -1,7 +1,6 @@ import sys import warnings -from django.db import transaction from django.db.utils import DatabaseError, OperationalError, ProgrammingError from netbox.plugins import PluginConfig @@ -33,7 +32,7 @@ def check_custom_object_type_table_exists(): table_name = CustomObjectType._meta.db_table cursor.execute(""" SELECT EXISTS ( - SELECT FROM information_schema.tables + SELECT FROM information_schema.tables WHERE table_name = %s ) """, [table_name])