X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FDBIx%2FClass.pm;h=c84f73ee9f2b7449b9a926b3771e548c9187d491;hb=e089c417ce04bb60a7d0644cb858f83049e64684;hp=d8f5344f3fbeed847333737dda2e21a8638a8d68;hpb=ac86dfe11b2d3d6b5f7a3c9dbbb2ddc31e86f315;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index d8f5344..c84f73e 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -163,8 +163,8 @@ sub parse { # global add_fk_index set in parser_args my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1; - foreach my $rel (sort @rels) - { + REL: + foreach my $rel (sort @rels) { my $rel_info = $source->relationship_info($rel); @@ -186,13 +186,18 @@ sub parse { # support quoting properly to be signaled about this $rel_table = $$rel_table if ref $rel_table eq 'SCALAR'; - my $reverse_rels = $source->reverse_relationship_info($rel); - my ($otherrelname, $otherrelationship) = each %{$reverse_rels}; - # Force the order of @cond to match the order of ->add_columns my $idx; my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->columns; - my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); + + for ( keys %{$rel_info->{cond}} ) { + unless (exists $other_columns_idx{$_}) { + carp "Ignoring relationship '$rel' - related resultsource does not contain one of the specified columns: '$_'\n"; + next REL; + } + } + + my @cond = sort { $other_columns_idx{$a} <=> $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); # Get the key information, mapping off the foreign/self markers my @refkeys = map {/^\w+\.(\w+)$/} @cond; @@ -217,6 +222,8 @@ sub parse { $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary); } + my ($otherrelname, $otherrelationship) = %{ $source->reverse_relationship_info($rel) }; + my $cascade; for my $c (qw/delete update/) { if (exists $rel_info->{attrs}{"on_$c"}) { @@ -252,9 +259,12 @@ sub parse { $tables{$table_name}{foreign_table_deps}{$rel_table}++; } + # trim schema before generating constraint/index names + (my $table_abbrev = $table_name) =~ s/ ^ [^\.]+ \. //x; + $table->add_constraint( type => 'foreign_key', - name => join('_', $table_name, 'fk', @keys), + name => join('_', $table_abbrev, 'fk', @keys), fields => \@keys, reference_fields => \@refkeys, reference_table => $rel_table, @@ -275,8 +285,9 @@ sub parse { next if join("\x00", @keys) eq join("\x00", @primary); if ($add_fk_index_rel) { + (my $idx_name = $table_name) =~ s/ ^ [^\.]+ \. //x; my $index = $table->add_index( - name => join('_', $table_name, 'idx', @keys), + name => join('_', $table_abbrev, 'idx', @keys), fields => \@keys, type => 'NORMAL', );