Subject: | might_have() relationships return bad data |
Bug: might_have relationships which depend on an attribute with
"is_nullable" being true can return incorrect data if that attribute is
NULL.
Peter Rabbitson mentions this bug in a mailing list response, but it
appears that people think the bug was eliminated. It's now causing
incorrect responses at my work.
http://lists.scsys.co.uk/pipermail/dbix-class/2009-December/008680.html
I've attached sample program to reproduce this error on DBIx::Class
version 0.08115. Note that even though we create a customer with *no*
account information (as is allowed by the code), the code still reports
the customer as having a "Premium" account.
Cheers,
Ovid
Subject: | might_have.pl |
#!/usr/bin/env perl
use strict;
use warnings;
my $db = 'premium.db';
my $sql = <<'END';
create table customer (
id int primary key,
account_id int null,
foreign key(account_id) references account(id)
);
create table account (
id int primary key,
type varchar(10)
);
insert into account values ( 1, 'Premium' );
END
unlink $db if -f $db;
open my $fh, "| sqlite3 $db" or die "Cannot pipe to sqlite3: $!";
print $fh $sql or die $!;
close $fh or die $!;
{
package The::Schema;
use parent qw/DBIx::Class::Schema/;
}
{
package The::Schema::Account;
use parent 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("account");
__PACKAGE__->add_columns(
id => { data_type => "int" },
type => { data_type => "varchar" },
);
__PACKAGE__->set_primary_key("id");
The::Schema->register_class( 'Account', __PACKAGE__ );
}
{
package The::Schema::Customer;
use parent 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("customer");
__PACKAGE__->add_columns(
id => { data_type => "int" },
account_id => { data_type => "int", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->might_have( "account", "The::Schema::Account" );
The::Schema->register_class( 'Customer', __PACKAGE__ );
}
my $schema = The::Schema->connect("dbi:SQLite:dbname=$db");
my $cust = $schema->resultset('Customer')->create( {} );
print $cust->account->type;