11import sys
22import warnings
33
4- from django .db import transaction
54from django .db .utils import DatabaseError , OperationalError , ProgrammingError
65from netbox .plugins import PluginConfig
76
@@ -24,18 +23,23 @@ def check_custom_object_type_table_exists():
2423 Check if the CustomObjectType table exists in the database.
2524 Returns True if the table exists, False otherwise.
2625 """
26+ from django .db import connection
2727 from .models import CustomObjectType
2828
2929 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
30+ # Use raw SQL to check table existence without generating ORM errors
31+ with connection .cursor () as cursor :
32+ table_name = CustomObjectType ._meta .db_table
33+ cursor .execute ("""
34+ SELECT EXISTS (
35+ SELECT FROM information_schema.tables
36+ WHERE table_name = %s
37+ )
38+ """ , [table_name ])
39+ table_exists = cursor .fetchone ()[0 ]
40+ return table_exists
3741 except (OperationalError , ProgrammingError , DatabaseError ):
38- # Catch database-specific errors (table doesn't exist, permission issues, etc.)
42+ # Catch database-specific errors (permission issues, etc.)
3943 return False
4044
4145
0 commit comments