This commit is contained in:
Louis Mazin 2026-09-20 01:11:15 +02:00
parent 9d23a03bc2
commit 78ec41ccb0

View File

@ -1,11 +1,25 @@
from django.db import migrations from django.db import migrations
def fix_user_foreign_key(apps, schema_editor): def debug_tables(apps, schema_editor):
connection = schema_editor.connection connection = schema_editor.connection
with connection.cursor() as cursor: with connection.cursor() as cursor:
# Récupère toutes les contraintes de time_tracking_timeentry
cursor.execute("SHOW CREATE TABLE `users_user`")
users_table = cursor.fetchone()
cursor.execute("SHOW CREATE TABLE `time_tracking_timeentry`")
timeentry_table = cursor.fetchone()
print("\n\n========== USERS_USER ==========")
print(users_table[1])
print("\n\n========== TIME_TRACKING_TIMEENTRY ==========")
print(timeentry_table[1])
print("\n\n========== FK INFO ==========")
cursor.execute(""" cursor.execute("""
SELECT SELECT
CONSTRAINT_NAME, CONSTRAINT_NAME,
@ -18,75 +32,30 @@ def fix_user_foreign_key(apps, schema_editor):
AND COLUMN_NAME = 'user_id' AND COLUMN_NAME = 'user_id'
""") """)
constraints = cursor.fetchall() for row in cursor.fetchall():
print(row)
print("DEBUG FK FOUND:", constraints) print("\n\n========== COLUMNS ==========")
old_constraint = None
for (
constraint_name,
column_name,
referenced_table,
referenced_column,
) in constraints:
if referenced_table:
old_constraint = constraint_name
print(
"DEBUG USER FK:",
constraint_name,
"->",
referenced_table,
referenced_column,
)
break
if old_constraint:
print(
f"Suppression de l'ancienne FK : {old_constraint}"
)
cursor.execute(
f"""
ALTER TABLE `time_tracking_timeentry`
DROP FOREIGN KEY `{old_constraint}`
"""
)
# Vérifie si la bonne FK existe déjà
cursor.execute(""" cursor.execute("""
SELECT SELECT
CONSTRAINT_NAME TABLE_NAME,
FROM information_schema.KEY_COLUMN_USAGE COLUMN_NAME,
COLUMN_TYPE,
IS_NULLABLE,
COLUMN_KEY,
COLLATION_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'time_tracking_timeentry' AND (
AND COLUMN_NAME = 'user_id' (TABLE_NAME = 'users_user' AND COLUMN_NAME = 'id')
AND REFERENCED_TABLE_NAME = 'users_user' OR
AND REFERENCED_COLUMN_NAME = 'id' (TABLE_NAME = 'time_tracking_timeentry' AND COLUMN_NAME = 'user_id')
)
""") """)
correct_constraint = cursor.fetchone() for row in cursor.fetchall():
print(row)
if correct_constraint:
print(
"La FK vers users_user existe déjà :",
correct_constraint[0],
)
return
print("Création de la FK vers users_user")
cursor.execute("""
ALTER TABLE `time_tracking_timeentry`
ADD CONSTRAINT `time_tracking_timeentry_user_id_users_user_fk`
FOREIGN KEY (`user_id`)
REFERENCES `users_user` (`id`)
ON DELETE CASCADE
""")
print("FK corrigée avec succès.")
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -97,7 +66,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.RunPython( migrations.RunPython(
fix_user_foreign_key, debug_tables,
migrations.RunPython.noop, migrations.RunPython.noop,
), ),
] ]