before_create trigger badness

[prev] [thread] [next] [Date index for 2004/10/08]

From: Christopher L. Everett
Subject: before_create trigger badness
Date: 10:11 on 08 Oct 2004
This is a multi-part message in MIME format.
--------------080000010004060601010502
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I have a situation where I do this:

Physemp::Model::Ad::User->create({ad_system_cookie => $ad_system_cookie,
                                  syndicator       => $syndicator});

And then my logs show that Class::DBI tries to run an insert with all
the columns:

[Fri Oct  8 03:20:44 2004] [error] Can't insert new 
Physemp::Model::Ad::User: DBD::mysql::st execute failed: Column 
'merged_to' cannot be null [for Statement "INSERT INTO physemp.ad_user 
(merged_to, address_3, current_as_of, recontact_period, 
area_choice_comments, address_2, email, postal_code, h_phone, 
syndicator, position_type, visa, name, full_name_date, ad_system_cookie, 
ad_user_id, additional_info, type, partial_name_date, w_phone, 
ad_session_id, status, address_1, school_location, non_us_state, city, 
us_state, country, locum, date_inserted, date_modified, deleted, 
send_job_listings, pager, merged, search_engine, year_completing_residency)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"] at /usr/local/share/perl/5.8.4/DBIx/ContextualFetch.pm line 51.
 at /usr/local/lib/site_perl/Physemp/Ad/Track.pm line 111

The right code IMO ought to have been

INSERT INTO physemp.ad_user
(ad_system_cookie, syndicator{, fields set in before_create triggers})
VALUES (?, ?{, place holders for fields set in before_create triggers});

I've attached a patch to CDBI which is less of an abortion than the last
solution I came up with for the problem, but IMO still not the right
thing at all.  Either not letting these spurious values get created in
the first place or inserting default values into the object after running
the before_create triggers would solve things more appropriately.

Unfortunately I can't track down where these spurious undef values get
inserted.  If someone gives me a clue about where that happens, I'd be
more than happy to generate a proper patch.

        -- 
        Christopher L. Everett

Chief Technology Officer                               www.medbanner.com
MedBanner, Inc.                                          www.physemp.com


--------------080000010004060601010502
Content-Type: text/x-patch;
 name="DBI.pm.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="DBI.pm.patch"

--- DBI.pm.orig	2004-10-08 04:45:24.000000000 -0500
+++ DBI.pm	2004-10-08 03:59:08.000000000 -0500
@@ -578,6 +578,9 @@
 	my $self = $class->_init($data);
 
 	$self->call_trigger('before_create');
+	delete $self->{$_} 
+	  foreach grep { not exists $self->{__Changed}{$_} and not exists $data->{$_} }
+	               $self->all_columns;
 	$self->call_trigger('deflate_for_create');
 
 	$self->_prepopulate_id if $self->_undefined_primary;

--------------080000010004060601010502--

before_create trigger badness
Christopher L. Everett 10:11 on 08 Oct 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52