[Dbix-class] Re: Unexpected behavior with possible NULL foreign key relationship

Adam Sjøgren adsj at novozymes.com
Wed Jun 4 10:42:28 BST 2008


On Sun, 20 Apr 2008 17:30:10 +0100, Matt wrote:

> Can I have another patch that doesn't break anything else please? :)

I just got surprised by this change in behaviour going from 0.08007 to
0.08008 (upgrading to Ubuntu 8.04 hardy): so attached is a go at
updating mike's patch to not break the other tests.

After applying it, I get this from a prove on all tests:

 Test Summary Report
 -------------------
 t/broken_single_accessor.t                       (Wstat: 256 Tests: 4 Failed: 1)
   Failed test number(s):  2
   Non-zero exit status: 1
 Files=136, Tests=2924, 83 wallclock secs ( 0.74 usr  0.25 sys + 75.95 cusr  5.10 csys = 82.04 CPU)
 Result: FAIL


The patch is against the latest 0.08/trunk (I hope that is the right
thing to do).


  Best regards,

    Adam

-- 
                                                          Adam Sjøgren
                                                    adsj at novozymes.com


diff --git a/t/60core.t b/t/60core.t
index 456dc7b..e5768bf 100644
--- a/t/60core.t
+++ b/t/60core.t
@@ -137,7 +137,7 @@ is($schema->resultset("Artist")->count, 4, 'count ok');
 my $cd = $schema->resultset("CD")->find(1);
 my %cols = $cd->get_columns;
 
-cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
+cmp_ok(keys %cols, '==', 5, 'get_columns number of columns ok');
 
 is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
 
@@ -153,7 +153,7 @@ $cd->discard_changes;
 # check whether ResultSource->columns returns columns in order originally supplied
 my @cd = $schema->source("CD")->columns;
 
-is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
+is_deeply( \@cd, [qw/cdid artist title year genreid/], 'column order');
 
 $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
 is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
@@ -319,9 +319,9 @@ ok(!$@, "stringify to false value doesn't cause error");
 
 # test remove_columns
 {
-  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]);
+  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year genreid/]);
   $schema->source('CD')->remove_columns('year');
-  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]);
+  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title genreid/]);
   ok(! exists $schema->source('CD')->_columns->{'year'}, 'year still exists in _columns');
 }
 
diff --git a/t/91debug.t b/t/91debug.t
index 68e7c57..ae059e4 100644
--- a/t/91debug.t
+++ b/t/91debug.t
@@ -55,7 +55,7 @@ open(STDERR, '>&STDERRCOPY');
     my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
     like(
         $sql,
-        qr/\QSELECT me.cdid, me.artist, me.title, me.year FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'\E/,
+        qr/\QSELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'\E/,
         'got correct SQL with all bind parameters'
     );
 }
diff --git a/t/broken_single_accessor.t b/t/broken_single_accessor.t
new file mode 100644
index 0000000..4f050b4
--- /dev/null
+++ b/t/broken_single_accessor.t
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+plan tests => 4;
+
+my $schema = DBICTest->init_schema();
+
+my $artist = $schema->resultset('Artist')->create({ artistid => 666, name => 'bad religion' });
+my $genre = $schema->resultset('Genre')->create({ name => 'disco' });
+my $cd = $schema->resultset('CD')->create({ cdid => 187, artist => 1,, title => 'how could hell be any worse?', year => 1982 });
+
+ok(!defined($cd->genreid), 'genreid is NULL');
+ok(!defined($cd->genre), 'genre accessor returns undef');
+
+$cd->discard_changes;
+
+ok(!defined($cd->genreid), 'genreid is NULL');
+ok(!defined($cd->genre), 'genre accessor returns undef');
diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm
index 22eddff..08f396f 100644
--- a/t/lib/DBICTest/Schema.pm
+++ b/t/lib/DBICTest/Schema.pm
@@ -11,6 +11,7 @@ __PACKAGE__->load_classes(qw/
   Employee
   CD
   FileColumn
+  Genre
   Link
   Bookmark
   #dummy
diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm
index 2530c7c..1720f87 100644
--- a/t/lib/DBICTest/Schema/CD.pm
+++ b/t/lib/DBICTest/Schema/CD.pm
@@ -20,6 +20,9 @@ __PACKAGE__->add_columns(
     data_type => 'varchar',
     size      => 100,
   },
+  'genreid' => {
+    data_type => 'integer'
+  }
 );
 __PACKAGE__->set_primary_key('cdid');
 __PACKAGE__->add_unique_constraint([ qw/artist title/ ]);
@@ -45,4 +48,11 @@ __PACKAGE__->many_to_many(
     { order_by => 'producer.name' },
 );
 
+__PACKAGE__->belongs_to('genre', 'DBICTest::Schema::Genre', { 'foreign.genreid' => 'self.genreid' });
+
+#__PACKAGE__->add_relationship('genre', 'DBICTest::Schema::Genre',
+#    { 'foreign.genreid' => 'self.genreid' },
+#    { 'accessor' => 'single' }
+#);
+
 1;
diff --git a/t/lib/DBICTest/Schema/Genre.pm b/t/lib/DBICTest/Schema/Genre.pm
new file mode 100644
index 0000000..284373d
--- /dev/null
+++ b/t/lib/DBICTest/Schema/Genre.pm
@@ -0,0 +1,11 @@
+package DBICTest::Schema::Genre;
+
+use strict;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('genre');
+__PACKAGE__->add_columns(qw/genreid name/);
+__PACKAGE__->set_primary_key('genreid');
+
+1;
diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql
index 5f17ebe..95f6ec3 100644
--- a/t/lib/sqlite.sql
+++ b/t/lib/sqlite.sql
@@ -90,7 +90,16 @@ CREATE TABLE cd (
   cdid INTEGER PRIMARY KEY NOT NULL,
   artist integer NOT NULL,
   title varchar(100) NOT NULL,
-  year varchar(100) NOT NULL
+  year varchar(100) NOT NULL,
+  genreid integer
+);
+
+--
+-- Table: genre
+--
+CREATE TABLE genre (
+  genreid INTEGER PRIMARY KEY NOT NULL,
+  name varchar(100) NOT NULL
 );
 
 --



More information about the DBIx-Class mailing list