Skip to content

Commit b2f12ac

Browse files
committed
avoid postgres errors pre migration run
1 parent 80371bd commit b2f12ac

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

netbox_custom_objects/__init__.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ def check_custom_object_type_table_exists():
2424
Check if the CustomObjectType table exists in the database.
2525
Returns True if the table exists, False otherwise.
2626
"""
27+
from django.db import connection
2728
from .models import CustomObjectType
2829

2930
try:
30-
# Try to query the model - if the table doesn't exist, this will raise an exception
31-
# this check and the transaction.atomic() is only required when running tests as the
32-
# migration check doesn't work correctly in the test environment
33-
with transaction.atomic():
34-
# Force immediate execution by using first()
35-
CustomObjectType.objects.first()
36-
return True
31+
# Use raw SQL to check table existence without generating ORM errors
32+
with connection.cursor() as cursor:
33+
table_name = CustomObjectType._meta.db_table
34+
cursor.execute("""
35+
SELECT EXISTS (
36+
SELECT FROM information_schema.tables
37+
WHERE table_name = %s
38+
)
39+
""", [table_name])
40+
table_exists = cursor.fetchone()[0]
41+
return table_exists
3742
except (OperationalError, ProgrammingError, DatabaseError):
38-
# Catch database-specific errors (table doesn't exist, permission issues, etc.)
43+
# Catch database-specific errors (permission issues, etc.)
3944
return False
4045

4146

0 commit comments

Comments
 (0)