From 73b85f25983eef60d0b767d2f56445cbad5ecb4b Mon Sep 17 00:00:00 2001 From: michaelschal <35957947-michaelschal@users.noreply.replit.com> Date: Mon, 8 Sep 2025 14:46:14 +0000 Subject: [PATCH] Fix foreign key constraint issue when creating new records Add a SQL script to ensure the existence of a default admin user and test data, addressing the foreign key constraint violation in the 'avoirs' table caused by missing 'created_by' references. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9fcf4b21-eb0c-4e53-a567-e2ce4a6ad869 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/9fcf4b21-eb0c-4e53-a567-e2ce4a6ad869/2Jd7tYp --- sql/fix_foreign_key_issue.sql | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 sql/fix_foreign_key_issue.sql diff --git a/sql/fix_foreign_key_issue.sql b/sql/fix_foreign_key_issue.sql new file mode 100644 index 0000000..4a9e4ac --- /dev/null +++ b/sql/fix_foreign_key_issue.sql @@ -0,0 +1,39 @@ +-- 🔧 CORRECTION CONTRAINTE FOREIGN KEY +-- Le problème : l'utilisateur référencé n'existe pas ! + +-- ÉTAPE 1: Voir les utilisateurs existants +SELECT 'UTILISATEURS EXISTANTS' as check_type; +SELECT id, username, role FROM users ORDER BY id; + +-- ÉTAPE 2: Si aucun utilisateur, créer un utilisateur admin +INSERT INTO users (id, username, email, role) +VALUES ('admin', 'admin', 'admin@test.com', 'admin') +ON CONFLICT (id) DO NOTHING; + +-- ÉTAPE 3: Insérer supplier et group de test si nécessaire +INSERT INTO suppliers (name) VALUES ('Test Supplier') ON CONFLICT DO NOTHING RETURNING id; +INSERT INTO groups (name) VALUES ('Test Group') ON CONFLICT DO NOTHING RETURNING id; + +-- ÉTAPE 4: Test avoir avec utilisateur existant +INSERT INTO avoirs ( + supplier_id, + group_id, + created_by, + invoice_reference, + amount, + comment +) VALUES ( + (SELECT id FROM suppliers WHERE name = 'Test Supplier' LIMIT 1), + (SELECT id FROM groups WHERE name = 'Test Group' LIMIT 1), + (SELECT id FROM users WHERE role = 'admin' LIMIT 1), -- Utilise un ID qui existe ! + 'TEST-FK-FIXED', + 99.99, + 'Test après correction foreign key' +) RETURNING id, created_by; + +-- ÉTAPE 5: Vérification +SELECT 'RÉSULTAT INSERTION' as check_type; +SELECT COUNT(*) as total_avoirs FROM avoirs; + +-- ÉTAPE 6: Nettoyage du test +DELETE FROM avoirs WHERE invoice_reference = 'TEST-FK-FIXED'; \ No newline at end of file