[Dbix-class] BUG? Unable to resolve relationship

Ovid publiustemp-dbic at yahoo.com
Fri Dec 11 14:55:07 GMT 2009


This is for DBIx::Class version 0.08115, but it happens with 08114.  This is the smallest test case I could get:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    my $db = 'test.db';
    my $sql = <<'END';
    create table customer (
        id    int primary key, 
        name  varchar(10) not null, 
        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 base qw/DBIx::Class::Schema::Loader/;
    }
    my $schema = The::Schema->connect("dbi:SQLite:dbname=$db");
    The::Schema::Customer->might_have( "account", "The::Schema::Account",
        { "foreign.id" => "self.account_id" } );
    my $cust = $schema->resultset('Customer')->create( { name => 'john' } );
    print $cust->account->type;

In DBIx::Class::VERSION 0.08010 (our current version at work), if you run this code and print $cust->account->type, and it prints "Premium", even though we have deliberately excluded an account.

When I upgrade to 0.08115:

  My::Schema::Customer::account(): Unable to resolve relationship from Customer to account: column account.account_id not loaded from storage (or not passed to new() prior to insert()). Maybe you forgot to call ->discard_changes to get defaults from the db. at loader.pl line 7

To me that seems wrong because customer.account is explicitly allowed to be NULL.  The generated code in customer is also correct in this respect (is_nullable is true):

  "account_id",
  { data_type => "int", default_value => undef, is_nullable => 1, size => undef },

Am I missing something here?

DBIx::Class::Schema::Loader: 0.04005
DBD::SQLite: 1.27

(I doubt these versions are relevant as this issue showed up in production code)

$ uname -a
SunOS ioz-dev-pipsv3 5.10 Generic_138889-03 i86pc i386 i86pc

Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://use.perl.org/~Ovid/journal/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6




More information about the DBIx-Class mailing list