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

Damon Snyder drsnyder at influxmedia.com
Fri Mar 21 22:14:40 GMT 2008


Hi Everyone,
I'm new to the list so bear with me if I'm missing something. I have a  
foreign key relationship where it's possible that the belongs_to  
relationship is NULL. See the classes below. When I create a new  
Smsmessage object e.g. like so:

my $sms2 = $m->resultset('Smsmessages')->create(
	{
		subject => 'Some subject',
		message => 'a message',
		channel_id => $chan2->id,
		#channel_keyword_id => undef,
		inception => scalar localtime(time()),
		expiration => scalar localtime(time() + (60*60*24)),
		sent => 0,
		retries => 0,
		provider_id => $prov->id,
	}
	);

If I leave out the channel_keyword_id => undef field, I will get a  
defined value for $sms2->channel_keyword. If I trace it, the following  
select ends up happening:

SELECT me.id, me.okid, me.keyword, me.channel_id FROM channel_keywords  
me:

Which returns a value that gets plugged the $sms2->channel_keyword  
field. Is this the expected behavior?  The default for this field is  
defined as undef below. If a value is not specified in the create()  
shouldn't it be given the default value (in this case NULL and not a  
random record)? I'm not quite sure I understand what is going on, but  
it seems like the new value is not getting an equivalent of NULL in  
the new object  unless I explicitly say channel_keyword_id => undef. I  
understand that it is good practice to set this value to NULL on  
creation, but I'm wondering if the behavior I'm seeing is to be  
expected.

Note that if I do set channel_keyword_id => undef, it behaves as  
expected and $sms2->channel_keyword is NULL.

Thanks,
Damon

#### Begin class with has_many #######
package RTD::Schema::RTDDB::ChannelKeywords;
use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("channel_keywords");
__PACKAGE__->add_columns(
   "id",
   { data_type => "INT", default_value => undef, is_nullable => 0,  
size => 11 },
   "okid",
   { data_type => "INT", default_value => "", is_nullable => 0, size  
=> 11 },
   "keyword",
   {
     data_type => "VARCHAR",
     default_value => undef,
     is_nullable => 1,
     size => 255,
   },
   "channel_id",
   { data_type => "INT", default_value => "", is_nullable => 0, size  
=> 11 },
);
__PACKAGE__->set_primary_key("id");


__PACKAGE__->belongs_to('channel' => 'RTD::Schema::RTDDB::Channels',  
'channel_id');

### This is the one giving me the problem
__PACKAGE__->has_many('smsmessages' =>  
'RTD::Schema::RTDDB::Smsmessages', 'channel_keyword_id');

########## end class with has many ######

######## begin class with belongs_to #####
package RTD::Schema::RTDDB::Smsmessages;
use strict;
use warnings;
use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("smsmessages");
__PACKAGE__->add_columns(
   "id",
   { data_type => "INT", default_value => undef, is_nullable => 0,  
size => 11 },
   "subject",
   {
     data_type => "VARCHAR",
     default_value => undef,
     is_nullable => 1,
     size => 128,
   },
   "message",
   { data_type => "VARCHAR", default_value => "", is_nullable => 0,  
size => 5000 },
   "channel_id",
   { data_type => "INT", default_value => 0, is_nullable => 0, size =>  
11 },
   "inception",
   {
     data_type => "DATETIME",
     default_value => "0000-00-00 00:00:00",
     is_nullable => 0,
     size => 19,
   },
   "expiration",
   {
     data_type => "DATETIME",
     default_value => "0000-00-00 00:00:00",
     is_nullable => 0,
     size => 19,
   },
   "sent",
   { data_type => "TINYINT", default_value => 0, is_nullable => 0,  
size => 4 },
   "retries",
   { data_type => "TINYINT", default_value => 0, is_nullable => 0,  
size => 4 },
   "provider_id",
   { data_type => "INT", default_value => 0, is_nullable => 0, size =>  
11 },
   "created_on",
   {
     data_type => "TIMESTAMP",
     default_value => "CURRENT_TIMESTAMP",
     is_nullable => 1,
     size => 14,
   },
   "updated_on",
   {
     data_type => "TIMESTAMP",
     default_value => "0000-00-00 00:00:00",
     is_nullable => 1,
     size => 14,
   },
   "channel_keyword_id", ### NOTE THE undef
   { data_type => "INT", default_value => undef, is_nullable => 1,  
size => 11 },
);
__PACKAGE__->set_primary_key("id");


__PACKAGE__->belongs_to('channel' => 'RTD::Schema::RTDDB::Channels',  
'channel_id');
__PACKAGE__->belongs_to('provider' => 'RTD::Schema::RTDDB::Providers',  
'provider_id');
### This is the one giving me problems
__PACKAGE__->belongs_to('channel_keyword' =>  
'RTD::Schema::RTDDB::ChannelKeywords',
							'channel_keyword_id', {join_type => 'left'}); 
              



More information about the DBIx-Class mailing list