     LEFT JOIN LATERAL (
       SELECT
         ct.confrelid AS m_table,
-        a.attname AS fk_m_column_name
+        fka.attname AS fk_m_column_name
       FROM
         pg_constraint ct
-      JOIN pg_attribute a
-        ON a.attrelid = ct.conrelid
-       AND a.attnum = ANY (ct.conkey)
+      JOIN pg_attribute fka
+        ON fka.attrelid = ct.conrelid
+       AND fka.attnum = ANY (ct.conkey)
       WHERE
         ct.conrelid = c.conrelid
-        AND ct.confrelid <> ($2 || '.' || $1)::regclass::oid
+        AND ct.confrelid <> c.conrelid
         AND ct.contype = 'f'
         AND ct.confdeltype = 'c'
     ) rt ON (true)
     -- count references of n:m tables
     -- this time, any FK type counts, > 1 ref = extra delete function
     LEFT JOIN LATERAL (
-      WITH RECURSIVE ref_table_depth(parent_oid, child_oid, depth) AS (
+      WITH RECURSIVE ref_table_depth(parent_table, ref_table, depth) AS (
         SELECT DISTINCT ON (conrelid)
-          confrelid AS parent_oid,
-          conrelid AS child_oid,
+          confrelid AS parent_table,
+          conrelid AS ref_table,
           1 AS depth
         FROM
           pg_constraint

          AND conrelid <> confrelid
          AND contype = 'f'
        UNION ALL
          SELECT DISTINCT ON (conrelid)
            c.confrelid AS parent_oid,
            c.conrelid AS child_oid,
            d.depth + 1 AS depth
          FROM
            pg_constraint c,
            ref_table_depth d
          WHERE
            d.child_oid = c.confrelid
            AND d.child_oid <> c.conrelid
            AND c.contype = 'f'
      )
      SELECT
        count(parent_oid) AS m_table_count,
        max(depth) AS m_table_depth
      FROM
        ref_table_depth
      GROUP BY
        parent_oid
    ) rtrf ON (true)
    -- check for FKs in n:m tables which cover same columns as PK
    -- if found = extra delete function to clean parent, except parent table = $1

    LEFT JOIN (
      -- count references of referencing tables
      -- > 1 ref = extra delete function
      WITH RECURSIVE ref_table_depth(parent_table, ref_table, depth) AS (
        SELECT DISTINCT ON (conrelid)
          confrelid AS parent_table,
          conrelid AS ref_table,
          1 AS depth
        FROM
          pg_constraint
