1
1
import sys
2
2
import warnings
3
3
4
- from django .db import transaction
5
4
from django .db .utils import DatabaseError , OperationalError , ProgrammingError
6
5
from netbox .plugins import PluginConfig
7
6
@@ -24,18 +23,23 @@ def check_custom_object_type_table_exists():
24
23
Check if the CustomObjectType table exists in the database.
25
24
Returns True if the table exists, False otherwise.
26
25
"""
26
+ from django .db import connection
27
27
from .models import CustomObjectType
28
28
29
29
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
37
41
except (OperationalError , ProgrammingError , DatabaseError ):
38
- # Catch database-specific errors (table doesn't exist, permission issues, etc.)
42
+ # Catch database-specific errors (permission issues, etc.)
39
43
return False
40
44
41
45
0 commit comments