-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_foreign_keys.py
More file actions
77 lines (62 loc) · 2.97 KB
/
debug_foreign_keys.py
File metadata and controls
77 lines (62 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
"""
Debug script to check foreign key constraints in SQLite database.
"""
import os
import sys
import django
# Setup Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'labitory.settings.development')
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
django.setup()
from django.db import connection
def check_foreign_keys():
print("Checking foreign key constraints referencing booking_booking table...")
with connection.cursor() as cursor:
# Get all tables
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [row[0] for row in cursor.fetchall()]
print(f"Found {len(tables)} tables")
# Check each table for foreign key references to booking_booking
foreign_key_references = []
for table in tables:
try:
cursor.execute(f"PRAGMA foreign_key_list({table})")
foreign_keys = cursor.fetchall()
for fk in foreign_keys:
# fk structure: (id, seq, table, from, to, on_update, on_delete, match)
if len(fk) >= 3 and fk[2] == 'booking_booking':
foreign_key_references.append({
'from_table': table,
'from_column': fk[3],
'to_table': fk[2],
'to_column': fk[4],
'on_delete': fk[6],
})
print(f" {table}.{fk[3]} -> {fk[2]}.{fk[4]} (ON DELETE {fk[6]})")
except Exception as e:
print(f"Error checking table {table}: {e}")
print(f"\nTotal foreign key references to booking_booking: {len(foreign_key_references)}")
# Now check for any records that reference booking ID 1
print(f"\nChecking for records referencing booking ID 1:")
booking_id = 1
for ref in foreign_key_references:
try:
table_name = ref['from_table']
column_name = ref['from_column']
query = f"SELECT COUNT(*) FROM {table_name} WHERE {column_name} = ?"
cursor.execute(query, [booking_id])
count = cursor.fetchone()[0]
if count > 0:
print(f" {table_name}.{column_name}: {count} records")
# Show the actual records
query2 = f"SELECT * FROM {table_name} WHERE {column_name} = ? LIMIT 5"
cursor.execute(query2, [booking_id])
records = cursor.fetchall()
print(f" Sample records: {records}")
except Exception as e:
print(f" Error checking {ref['from_table']}.{ref['from_column']}: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
check_foreign_keys()