[Dbix-class] is_foreign_key_constraint => false is ineffective

Jon Schutz jon+dbix at youramigo.com
Fri Nov 2 06:05:14 GMT 2007

I've added is_foreign_key_constraint => 0 as an attribute in a
relationship, in order to query across the join without imposing
database level constraints - but the constraint is still appearing in
the resulting schema.

The doco in Relationship::Base says "set this attribute to a true or
false value to override the detection of when to create constraints."

Can anyone (particular Jess & Matt whose names can be found in the
source) explain the logic in the following bit of code to me?  From
SQL::Translator::Parser::DBIx::Class.pm :

               # Make sure we dont create the same foreign key constraint twice
                # Decide if this is a foreign key based on whether the self
                # items are our primary columns.
                # If the sets are different, then we assume it's a foreign key from
                # us to another table.
                # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
                if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
                     ( exists $rel_info->{attrs}{is_foreign_key_constraint} && 
                       $rel_info->{attrs}{is_foreign_key_constraint} ||
                       !$source->compare_relationship_keys(\@keys, \@primary)
                    $created_FK_rels{$rel_table}->{$key_test} = 1;

I read it as - if the foreign key has already been created, don't try to
recreate the same one.  If is_foreign_key_constraint has been specified
and is set, or if the keys are not primary keys, then create the foreign
key relation.  So it seems we can force foreign keys with
is_foreign_key_constraint = true, but can't forcibly disable them as the
compare_relationship_keys test wins in the end.

Shouldn't that logic be : if is_foreign_key_constraint has been
specified then do whatever it says, otherwise test using
compare_relationship_keys ? i.e.

               if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
		     ( exists $rel_info->{attrs}{is_foreign_key_constraint} ? 
			$rel_info->{attrs}{is_foreign_key_constraint} :
			$source->compare_relationship_keys(\@keys, \@primary) 
		   ) ...



More information about the DBIx-Class mailing list