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

Octavian Rasnita orasnita at gmail.com
Sat Mar 24 19:01:32 GMT 2007


A key could force a column to have unique values, but those values could be 
null if I remember well, and if there are more null values, I don't know if 
that column can be used for a relationship...

Octavian

----- Original Message ----- 
From: "Matija Grabnar" <matija at serverflow.com>
To: <dbix-class at lists.rawmode.org>
Sent: Saturday, March 24, 2007 7:56 PM
Subject: [Dbix-class] Re: has_many/belongs_to relationship to non-primary 
key


> 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]",
>
>
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> Searchable Archive: 
> http://www.mail-archive.com/dbix-class@lists.rawmode.org/ 




More information about the Dbix-class mailing list