         all_constraints p
         ON p.constraint_name = fk.r_constraint_name
        AND p.owner = fk.owner
-      WHERE fk.table_name = upper(tab_name)
+      WHERE
+        fk.table_name = upper(tab_name)
         AND fk.owner = upper(schema_name)
         AND fk.constraint_type = 'R'
         AND fk.delete_rule = 'NO ACTION'
+        AND pk.position = 1
     )
     LOOP
       -- create array delete function for parent table

    schema_name VARCHAR2 := USER
    )
  IS
    ddl_command VARCHAR2(10000) := 'CREATE OR REPLACE FUNCTION delete_'||tab_short_name||'(pid NUMBER) RETURN NUMBER'||chr(10)||'IS'||chr(10);
    declare_block VARCHAR2(500) := chr(10)||''||chr(10)||'  deleted_id NUMBER;';
    pre_block VARCHAR2(2000) := '';
    post_block VARCHAR2(1000) := '';
    delete_block VARCHAR2(1000) := chr(10)||'  DELETE FROM'||chr(10)||'    '||tab_name||chr(10)||'  WHERE'||chr(10)||'    id = pid'||chr(10)||'  RETURNING'||chr(10)||'    id';
    delete_into_block VARCHAR2(500) := chr(10)||'  INTO'||chr(10)||'    deleted_id';
    vars VARCHAR2(500);
    ref_block VARCHAR2(2000);
    returning_block VARCHAR2(500);

        p.table_name AS parent_table_short
      FROM
        all_constraints fk
      JOIN
        all_cons_columns fka
        ON fka.constraint_name = fk.constraint_name
       AND fka.table_name = fk.table_name
       AND fka.owner = fk.owner
      JOIN (
        SELECT DISTINCT
          ctp.table_name,
          ctp.owner,
          first_value(pka.column_name) OVER (PARTITION BY ctp.table_name ORDER BY pka.position DESC) AS column_name,
          first_value(pka.position) OVER (PARTITION BY ctp.table_name ORDER BY pka.position DESC) AS position
        FROM
          all_constraints ctp
        JOIN
          all_cons_columns pka
          ON pka.constraint_name = ctp.constraint_name
         AND pka.table_name = ctp.table_name
         AND pka.owner = ctp.owner
