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
This commit is contained in:
michaelschal committed 2025-09-08 14:46:14 +00:00
1 parent 6f09a7abc4
commit 73b85f2598
1 file changed
+39
+39
View File
@@ -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';