From: Dagfinn Ilmari Mannsåker Date: Mon, 21 May 2007 18:33:33 +0000 (+0000) Subject: fix multiple multi-column relations to the same table X-Git-Tag: 0.03012~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=25e3e91dc64a2465d1ae8e3baac4d50d9c6ba0ce;p=dbsrgits%2FDBIx-Class-Schema-Loader.git fix multiple multi-column relations to the same table implementation by Brandon L Black, tests by me --- diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index 9b61371..5d22601 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -161,21 +161,11 @@ sub generate_code { $cond{$remote_cols->[$i]} = $local_cols->[$i]; } - # If more than one rel between this pair of tables, use the - # local col name(s) as the relname in the foreign source, instead - # of the local table name. my $local_relname; - if($counters{$remote_moniker} > 1) { - $local_relname = $self->_inflect_plural( - lc($local_table) . q{_} . join(q{_}, @$local_cols) - ); - } else { - $local_relname = $self->_inflect_plural(lc $local_table); - } - - # for single-column case, set the relname to the column name, - # to make filter accessors work my $remote_relname; + + # for single-column case, set the remote relname to the column + # name, to make filter accessors work if(scalar keys %cond == 1) { my ($col) = keys %cond; $remote_relname = $self->_inflect_singular($cond{$col}); @@ -184,6 +174,18 @@ sub generate_code { $remote_relname = $self->_inflect_singular(lc $remote_table); } + # If more than one rel between this pair of tables, use the local + # col names to distinguish + if($counters{$remote_moniker} > 1) { + my $colnames = q{_} . join(q{_}, @$local_cols); + $local_relname = $self->_inflect_plural( + lc($local_table) . $colnames + ); + $remote_relname .= $colnames if keys %cond > 1; + } else { + $local_relname = $self->_inflect_plural(lc $local_table); + } + my %rev_cond = reverse %cond; for (keys %rev_cond) { diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index aa32df0..f15e6f3 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -43,7 +43,7 @@ sub _monikerize { sub run_tests { my $self = shift; - plan tests => 76; + plan tests => 84; $self->create(); @@ -297,6 +297,14 @@ sub run_tests { my $class22 = $classes->{loader_test22}; my $rsobj22 = $conn->resultset($moniker22); + my $moniker25 = $monikers->{loader_test25}; + my $class25 = $classes->{loader_test25}; + my $rsobj25 = $conn->resultset($moniker25); + + my $moniker26 = $monikers->{loader_test26}; + my $class26 = $classes->{loader_test26}; + my $rsobj26 = $conn->resultset($moniker26); + isa_ok( $rsobj3, "DBIx::Class::ResultSet" ); isa_ok( $rsobj4, "DBIx::Class::ResultSet" ); isa_ok( $rsobj5, "DBIx::Class::ResultSet" ); @@ -311,6 +319,8 @@ sub run_tests { isa_ok( $rsobj20, "DBIx::Class::ResultSet" ); isa_ok( $rsobj21, "DBIx::Class::ResultSet" ); isa_ok( $rsobj22, "DBIx::Class::ResultSet" ); + isa_ok( $rsobj25, "DBIx::Class::ResultSet" ); + isa_ok( $rsobj26, "DBIx::Class::ResultSet" ); # basic rel test my $obj4 = $rsobj4->find(123); @@ -353,6 +363,22 @@ sub run_tests { # XXX test double-fk m:m 21 <- 22 -> 21 + # test double multi-col fk 26 -> 25 + my $obj26 = $rsobj26->find(33); + + my $rs_rel25_one = $obj26->loader_test25_id_rel1; + isa_ok($rs_rel25_one, $class25); + is($rs_rel25_one->dat, 'x25'); + + my $rs_rel25_two = $obj26->loader_test25_id_rel2; + isa_ok($rs_rel25_two, $class25); + is($rs_rel25_two->dat, 'y25'); + + my $obj25 = $rsobj25->find(3,42); + my $rs_rel26 = $obj25->search_related('loader_test26_id_rel1s'); + isa_ok($rs_rel26->first, $class26); + is($rs_rel26->first->id, 3); + # from Chisel's tests... SKIP: { if($self->{vendor} =~ /sqlite/i) { @@ -671,6 +697,32 @@ sub create { q{ INSERT INTO loader_test22 (parent, child) VALUES (7,11)}, q{ INSERT INTO loader_test22 (parent, child) VALUES (11,13)}, q{ INSERT INTO loader_test22 (parent, child) VALUES (13,17)}, + + qq{ + CREATE TABLE loader_test25 ( + id1 INTEGER NOT NULL, + id2 INTEGER NOT NULL, + dat VARCHAR(8), + PRIMARY KEY (id1,id2) + ) $self->{innodb} + }, + + q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,5,'x25') }, + q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (33,7,'y25') }, + q{ INSERT INTO loader_test25 (id1,id2,dat) VALUES (3,42,'z25') }, + + qq{ + CREATE TABLE loader_test26 ( + id INTEGER NOT NULL PRIMARY KEY, + rel1 INTEGER NOT NULL, + rel2 INTEGER NOT NULL, + FOREIGN KEY (id, rel1) REFERENCES loader_test25 (id1, id2), + FOREIGN KEY (id, rel2) REFERENCES loader_test25 (id1, id2) + ) $self->{innodb} + }, + + q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (33,5,7) }, + q{ INSERT INTO loader_test26 (id,rel1,rel2) VALUES (3,42,42) }, ); my @statements_advanced = ( @@ -795,6 +847,8 @@ sub drop_tables { loader_test18 loader_test22 loader_test21 + loader_test26 + loader_test25 /; my @tables_advanced = qw/