[Dbix-class] Re: has_many/belongs_to relationship to non-primary key

Matija Grabnar matija at serverflow.com
Sat Mar 24 17:56:55 GMT 2007


Since nobody posted any reasons why I shouldn't do this, I'm attaching a 
patch for SQL::Translator::Parser::DBIx::Class which makes it possible 
to have a has_many referenced from a non-primary key, as long as that 
key is constrained to be unique.

I believe that this is sufficient condition for relationships to work 
correctly, and in fact, I need this for a  project I am working on.

Please, tell me if there is anything more I need to do to get the patch 
accepted.

--- Class.pm.old        2007-01-15 12:14:32.000000000 +0100
+++ Class.pm    2007-03-24 12:20:35.000000000 +0100
@@ -121,11 +121,18 @@
                 }
 
                 #Decide if this is a foreign key based on whether the self
-                #items are our primary columns.
+                #items are our unique columns.
 
                 # If the sets are different, then we assume it's a 
foreign key
from
                 # us to another table.
-                if (!$source->compare_relationship_keys(\@keys, 
\@primary)) {
+                my $has_unique=$source->
+                  compare_relationship_keys(\@keys, \@primary);
+                foreach my $uniq (keys %unique_constraints) {
+                  last if $has_unique;
+                  $has_unique = 1 if $source->compare_relationship_keys
+                    (\@keys, $unique_constraints{$uniq});
+                }
+                if (! $has_unique) {
                     $table->add_constraint(
                                 type             => 'foreign_key',
                                 name             => "fk_$keys[0]",




More information about the Dbix-class mailing list