 LANGUAGE sql STRICT;
 
 -- creates code block to delete referenced entries in n table with function call
-CREATE OR REPLACE FUNCTION citydb_pkg.delete_n_table_by_ids(
-  table_name TEXT,
-  table_short_name TEXT,
+CREATE OR REPLACE FUNCTION citydb_pkg.generate_delete_ref_by_id_call(
+  table_name TEXT,
   fk_column_name TEXT
-  ) RETURNS TEXT AS 
-$$
-SELECT
-  E'\n  -- delete '||$1||'s'
+  ) RETURNS TEXT AS
+$$
+SELECT
+     E'\n  -- delete '||$1||'s'
   || E'\n  PERFORM'
-  || E'\n    gen.delete_'||$2||E'(array_agg(t.id))'
+  || E'\n    gen.delete_'||citydb_pkg.get_short_name($1)||E'(array_agg(t.id))'
   || E'\n  FROM'
   || E'\n    '||$1|| ' t,'
   || E'\n    unnest($1) a(a_id)'
   || E'\n  WHERE'
-  || E'\n    t.'||$3||E' = a.a_id;\n';
+  || E'\n    t.'||$2||E' = a.a_id;\n';
 $$
 LANGUAGE sql STRICT;
 
 -- creates code block to delete unreferenced entries in m table
-CREATE OR REPLACE FUNCTION citydb_pkg.delete_m_table_by_ids(
+CREATE OR REPLACE FUNCTION citydb_pkg.generate_delete_m_ref_by_id_stmt(
   m_table_name TEXT,
   fk_m_column_name TEXT,
   n_m_table_name TEXT
   ) RETURNS TEXT AS 
 $$
 SELECT
-  E'\n  -- delete '||$1||'(s) not being referenced any more'
-  || E'\n  IF -1 = ALL('||$1||'_ids) IS NOT NULL THEN'
+     E'\n  -- delete '||$1||'(s) not being referenced any more'
+  || E'\n  IF -1 = ALL('||citydb_pkg.get_short_name($1)||'_ids) IS NOT NULL THEN'
   || E'\n    DELETE FROM'
   || E'\n      '||$1||' m'
   || E'\n    USING'
-  || E'\n      (SELECT DISTINCT unnest('||$1||'_ids) AS a_id) a'
+  || E'\n      (SELECT DISTINCT unnest('||citydb_pkg.get_short_name($1)||'_ids) AS a_id) a'
   || E'\n    LEFT JOIN'
   || E'\n     '||$3||' n2m'
   || E'\n      ON n2m.'||$2||' = a.a_id'

LANGUAGE sql STRICT;

-- creates code block to delete unreferenced entries in m table with function call
CREATE OR REPLACE FUNCTION citydb_pkg.delete_m_table_by_id(
  m_table_name TEXT,
  m_table_short_name TEXT,
  fk_m_column_name TEXT,
  n_m_table_name TEXT
  ) RETURNS TEXT AS 
$$
SELECT
  E'\n  -- delete '||$1||'(s) not being referenced any more'
  || E'\n  IF '||$1||'_ref_id IS NOT NULL THEN'
  || E'\n    PERFORM'
  || E'\n      gen.delete_'||$2||'(a.a_id)'
  || E'\n    FROM'
  || E'\n      (VALUES ('||$1||'_ref_id)) a(a_id)'
  || E'\n    LEFT JOIN'
  || E'\n     '||$4||' n2m'
  || E'\n      ON n2m.'||$3||' = a.a_id'
  || E'\n    WHERE'
  || E'\n      n2m.'||$3||' IS NULL;'
  || E'\n  END IF;\n';
$$
LANGUAGE sql STRICT;

-- creates code block to delete referenced entries in n:m table
-- adds another code block to delete unreferenced entries in m table
CREATE OR REPLACE FUNCTION citydb_pkg.delete_n_m_table_by_id(
  n_m_table_name TEXT,
  fk_n_column_name TEXT,
  m_table_name TEXT,
  fk_m_column_name TEXT
  ) RETURNS TEXT AS 
$$
SELECT E'\n  -- delete references to '||$3||'s'
  || E'\n  WITH delete_'||$3||'_refs AS ('
  || E'\n    DELETE FROM'
  || E'\n      '||$1
  || E'\n    WHERE'

    declare_block,
    pre_block
  FROM
    citydb_pkg.create_ref_delete($1, $2);

  -- MAIN DELETE
  delete_block := generate_delete_by_id_stmt($1);

  -- FOREIGN KEY which are set to ON DELETE SET NULL
  SELECT
