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

Matt S Trout dbix-class at trout.me.uk
Sun Apr 6 20:08:38 BST 2008


On Fri, Mar 21, 2008 at 03:14:40PM -0700, Damon Snyder wrote:
> 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.

That's very odd. Could you try and produce a minimal set of classes that
exhibit this or better still make it happen against the DBICTest schema
in the DBIx::Class dist?

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the DBIx-Class mailing list