in reply to DBI:to sort tables according to their dependencies

Assuming you have a way to retrieve the dependencies for each table, you are only a topological sort away from achieving your goal.

  • Comment on Re: DBI:to sort tables according to their dependencies

Replies are listed 'Best First'.
Re^2: DBI:to sort tables according to their dependencies
by ansh batra (Friar) on Dec 21, 2011 at 10:38 UTC

    thanks marto .
    can you suggest me any way of getting the dependencies also

      I'm sure you can search the WWW. "postgres list foreign keys" proved successful for me. (I'm not allowed to link to external sites as anonymous)

      Furthermore, you can run psql -E and use the backslash-functions (e.g. \d) and watch the SQL it issues to figure out what fetches the foreign keys.

        oops sorry moritz :)
        i have this with me
        my $sth = $dbh->prepare("select tablename from pg_tables where schema +name=?"); $sth->execute($schema); my @tables; while (my @t = $sth->fetchrow_array) { push @tables, @t; } $sth->finish;
        and from the example in Sort::Topological i have
        my %children = ( 'a' => [ 'b', 'c' ], 'c' => [ 'x' ], 'b' => [ 'x' ], 'x' => [ 'y' ], 'y' => [ 'z' ], 'z' => [ ], ); sub children { @{$children{$_[0]} || []}; } my @unsorted = ( 'z', 'a', 'x', 'c', 'b', 'y' ); my @sorted = toposort(\&children, \@unsorted);
        so i am planning something like foreach element in @tables i should call get_deps which will return the contents to be put inside a => 'here'