   ref.n_table_name::regclass::text,
   ref.n_fk_column_name,
   ref.cleanup_n_table,
-  CASE WHEN root_table_name = cleanup_n_table THEN 1 ELSE 0 END AS is_child,
+  CASE WHEN ref.root_table_name = ref.cleanup_n_table THEN 1 ELSE 0 END AS is_child,
   m.m_table_name::regclass::text,
   m.m_fk_column_name::text,
   m.m_ref_column_name::text,

      vars := vars || E'\n  ' || citydb_pkg.get_short_name(rec.ref_table_name) || '_ids int[] := ''{}'';';
      returning_block := returning_block || ','
        || E'\n    ARRAY['
        || E'\n    ' || array_to_string(rec.fk_columns, E',\n    ')
        || E'\n    ]';
      into_block := into_block || E',\n    ' || citydb_pkg.get_short_name(rec.ref_table_name)||'_ids';
    ELSE
      vars := vars || E'\n  ' || citydb_pkg.get_short_name(rec.ref_table_name)||'_ref_id int;';
      returning_block := returning_block || E',\n      ' || array_to_string(rec.fk_columns, E',\n      ');
      into_block := into_block || E',\n    ' || citydb_pkg.get_short_name(rec.ref_table_name) || '_ref_id';
    END IF;


  schema_name TEXT DEFAULT 'citydb'
  ) RETURNS TEXT AS
$$
SELECT
  f.confrelid::regclass::text AS parent_table
FROM
  pg_constraint f,
