On Sat, 2007-11-03 at 13:15 +0000, Ash Berlin wrote:
> Jon Schutz wrote:
> > On Fri, 2007-11-02 at 09:31 +0000, Ash Berlin wrote:
> > 
> >> Yes good point. Of course I only ever wanted it to force creating of
> >> keys, not stop it. Fancy putting together some test cases for this? (By
> >> creating a patch against t/86sqlt.t and other files?
> >>
> > 
> > Patch against svn inc tests attached.
> > 
> > Regards,
> 
> Looks okay apart from you didn't include ForceForeign.pm ;)

Oops, attached as separate file, and in revised patch file.

-- 

Jon

Index: t/lib/DBICTest/Schema/ForceForeign.pm
===================================================================
--- t/lib/DBICTest/Schema/ForceForeign.pm	(revision 0)
+++ t/lib/DBICTest/Schema/ForceForeign.pm	(revision 0)
@@ -0,0 +1,32 @@
+package # hide from PAUSE
+    DBICTest::Schema::ForceForeign;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('forceforeign');
+__PACKAGE__->add_columns(
+  'artist' => { data_type => 'integer' },
+  'cd' => { data_type => 'integer' },
+);
+__PACKAGE__->set_primary_key(qw/artist/);
+
+# Normally this would not appear as a FK constraint
+# since it uses the PK
+__PACKAGE__->might_have(
+			'artist_1', 'DBICTest::Schema::Artist', {
+			    'foreign.artist_id' => 'self.artist',
+			}, {
+			    is_foreign_key_constraint => 1,
+			},
+);
+
+# Normally this would appear as a FK constraint
+__PACKAGE__->might_have(
+			'cd_1', 'DBICTest::Schema::CD', {
+			    'foreign.cdid' => 'self.cd',
+			}, {
+			    is_foreign_key_constraint => 0,
+			},
+);
+
+1;
Index: t/lib/DBICTest/Schema.pm
===================================================================
--- t/lib/DBICTest/Schema.pm	(revision 3852)
+++ t/lib/DBICTest/Schema.pm	(working copy)
@@ -36,7 +36,8 @@
   ),
   qw/SelfRefAlias TreeLike TwoKeyTreeLike Event EventTZ NoPrimaryKey/,
   qw/Collection CollectionObject TypedObject/,
-  qw/Owners BooksInLibrary/
+  qw/Owners BooksInLibrary/,
+  qw/ForceForeign/  
 );
 
 sub sqlt_deploy_hook {
Index: t/86sqlt.t
===================================================================
--- t/86sqlt.t	(revision 3852)
+++ t/86sqlt.t	(working copy)
@@ -10,7 +10,7 @@
 
 my $schema = DBICTest->init_schema;
 
-plan tests => 56;
+plan tests => 60;
 
 my $translator = SQL::Translator->new( 
   parser_args => {
@@ -176,6 +176,16 @@
       on_delete => '', on_update => '',
     },
   ],
+  # ForceForeign
+  forceforeign => [
+    {
+      'display' => 'forceforeign->artist',
+      'selftable' => 'forceforeign', 'foreigntable' => 'artist', 
+      'selfcols'  => ['artist'], 'foreigncols' => ['artist_id'], 
+      on_delete => '', on_update => '',
+    },
+  ],
+
 );
 
 my %unique_constraints = (
@@ -222,7 +232,6 @@
 );
 
 my $tschema = $translator->schema();
-
 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
 # the 'link' table
 ok( !defined($tschema->get_table('link')), "Link table was removed by hook");
@@ -232,6 +241,8 @@
 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
+$constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
+ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
 
 for my $expected_constraints (keys %fk_constraints) {
   for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
Index: lib/SQL/Translator/Parser/DBIx/Class.pm
===================================================================
--- lib/SQL/Translator/Parser/DBIx/Class.pm	(revision 3852)
+++ lib/SQL/Translator/Parser/DBIx/Class.pm	(working copy)
@@ -149,10 +149,10 @@
                 # 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} ||
+                     ( exists $rel_info->{attrs}{is_foreign_key_constraint} ?
+                       $rel_info->{attrs}{is_foreign_key_constraint} :
                        !$source->compare_relationship_keys([EMAIL PROTECTED], [EMAIL PROTECTED])
-                     )
+		     )
                    )
                 {
                     $created_FK_rels{$rel_table}->{$key_test} = 1;

Attachment: ForceForeign.pm
Description: Perl program

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/[EMAIL PROTECTED]

Reply via email to