From 15d7d0e27a6ef4ccdf6e9aee82afae0173cbbfcd Mon Sep 17 00:00:00 2001 From: Michael Conrad Date: Wed, 21 Dec 2022 07:17:46 -0500 Subject: [PATCH] Fix bug loading Postgres index fields When parsing indices of a table, the Parser::DBI::PostgreSQL makes an incorrect assumption that the numeric field references refer to the current column order. This breaks if the table has had any columns deleted, because Postgres does not re-number the columns, so the column (attribute ids) might be (1,2,4,6,7) and these are the numbers that need used. The code already retrieved the ids of the columns, so just store them in a hash and use them instead of the column name list. --- lib/SQL/Translator/Parser/DBI/PostgreSQL.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm index 90782668..c751fc5d 100644 --- a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm @@ -119,6 +119,7 @@ ORDER BY 1; $column_select->execute($table_oid); + my %column_by_attrid; while (my $columnhash = $column_select->fetchrow_hashref ) { #data_type seems to not be populated; perhaps there needs to @@ -135,17 +136,17 @@ ORDER BY 1; if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF; $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1; $col->comments($$columnhash{'description'}) if $$columnhash{'description'}; + $column_by_attrid{$$columnhash{'attnum'}}= $$columnhash{'attname'}; } $index_select->execute($table_oid); - my @column_names = $table->field_names(); while (my $indexhash = $index_select->fetchrow_hashref ) { #don't deal with function indexes at the moment next if ($$indexhash{'indkey'} eq '' or !defined($$indexhash{'indkey'}) ); - my @columns = map $column_names[$_ - 1], split /\s+/, $$indexhash{'indkey'}; + my @columns = map $column_by_attrid{$_}, split /\s+/, $$indexhash{'indkey'}; my $type; if ($$indexhash{'indisprimary'}) {