Mahnungs-Tabelle: Fremdschlüsselverknüpfung auf Rechnungstabelle
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 19 Jul 2019 13:16:25 +0000 (15:16 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 19 Jul 2019 13:17:11 +0000 (15:17 +0200)
SL/DB/MetaSetup/Dunning.pm
scripts/rose_auto_create_model.pl
sql/Pg-upgrade2/dunning_foreign_key_for_trans_id.sql [new file with mode: 0644]

index a0098e7..ee6ab4e 100644 (file)
@@ -33,10 +33,15 @@ __PACKAGE__->meta->foreign_keys(
     key_columns => { dunning_config_id => 'id' },
   },
 
-  fee_interest_ar => {
+  fee_interest_invoice => {
     class       => 'SL::DB::Invoice',
     key_columns => { fee_interest_ar_id => 'id' },
   },
+
+  invoice => {
+    class       => 'SL::DB::Invoice',
+    key_columns => { trans_id => 'id' },
+  },
 );
 
 1;
index 5a0f7c2..2ab34cb 100755 (executable)
@@ -82,6 +82,8 @@ our %foreign_key_name_map     = (
 
     assembly                  => { parts_id => 'part', id => 'assembly_part' },
     assortment_items          => { parts_id => 'part' },
+
+    dunning                   => { trans_id => 'invoice', fee_interest_ar_id => 'fee_interest_invoice' },
   },
 );
 
diff --git a/sql/Pg-upgrade2/dunning_foreign_key_for_trans_id.sql b/sql/Pg-upgrade2/dunning_foreign_key_for_trans_id.sql
new file mode 100644 (file)
index 0000000..e84892e
--- /dev/null
@@ -0,0 +1,14 @@
+-- @tag: dunning_foreign_key_for_trans_id
+-- @description: Mahnungstabelle: Fremdschlüssel für Verknüpfung zur Rechnungstabelle
+-- @depends: release_3_5_3
+DELETE FROM dunning
+WHERE NOT EXISTS (
+  SELECT ar.id
+  FROM ar
+  WHERE ar.id = dunning.trans_id
+);
+
+ALTER TABLE dunning
+ADD CONSTRAINT dunning_trans_id_fkey
+FOREIGN KEY (trans_id) REFERENCES ar (id)
+ON DELETE CASCADE;