subtotaltitle/sql/fix_foreign_keys.sql
2026-01-26 19:48:26 +01:00

30 lines
1.5 KiB
SQL

-- Fix Foreign Key Constraints für Multi-Document-Support
-- Diese Felder sollten NULL sein können, da eine Section entweder zu einer
-- Rechnung ODER einem Angebot ODER einem Auftrag gehört, aber nie zu allen gleichzeitig.
-- 1. Foreign Key Constraints temporär entfernen
ALTER TABLE llx_facture_lines_manager DROP FOREIGN KEY IF EXISTS `1`;
ALTER TABLE llx_facture_lines_manager DROP FOREIGN KEY IF EXISTS `2`;
ALTER TABLE llx_facture_lines_manager DROP FOREIGN KEY IF EXISTS `3`;
ALTER TABLE llx_facture_lines_manager DROP FOREIGN KEY IF EXISTS `fk_facture_lines_manager_facture`;
ALTER TABLE llx_facture_lines_manager DROP FOREIGN KEY IF EXISTS `fk_facture_lines_manager_propal`;
ALTER TABLE llx_facture_lines_manager DROP FOREIGN KEY IF EXISTS `fk_facture_lines_manager_commande`;
-- 2. Felder auf NULL ändern
ALTER TABLE llx_facture_lines_manager
MODIFY COLUMN fk_facture INT NULL DEFAULT NULL,
MODIFY COLUMN fk_propal INT NULL DEFAULT NULL,
MODIFY COLUMN fk_commande INT NULL DEFAULT NULL;
-- 3. Foreign Key Constraints wieder hinzufügen (mit ON DELETE CASCADE)
ALTER TABLE llx_facture_lines_manager
ADD CONSTRAINT fk_facture_lines_manager_facture
FOREIGN KEY (fk_facture) REFERENCES llx_facture(rowid) ON DELETE CASCADE;
ALTER TABLE llx_facture_lines_manager
ADD CONSTRAINT fk_facture_lines_manager_propal
FOREIGN KEY (fk_propal) REFERENCES llx_propal(rowid) ON DELETE CASCADE;
ALTER TABLE llx_facture_lines_manager
ADD CONSTRAINT fk_facture_lines_manager_commande
FOREIGN KEY (fk_commande) REFERENCES llx_commande(rowid) ON DELETE CASCADE;