Ovid wrote:
> 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.
This was a bug.
>
> 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
This is correct and expected. Read the error message more carefully. To break
it down:
*) You create a row, without specifying a value for account_id.
*) The resulting object has only a name, and its autoinc id, dbic does not
assume
that the value of account_id is NULL/undef, it simply isn't there (i.e. the
db-side
default may be 0, or 42 or 'kinky sex')
*) Since there is no value for the FK, the relationship can not be resolved and
subsequently the exception you see is thrown.
*) If you do what has been suggeste d- call discard_changes to make a db-trip to
retrieve the missing values - everything will just work. Alternatively you can
create() the row explici5tly specifying account_id => undef - this will work as
expected too.
Alo the relationship naming is a bot unfortunate, the documentation indeed needs
some improvement. Should be available in the next release.
_______________________________________________
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]