This is one of those issues you hope you don't encounter more than once a year...

Try this:

Setup a database with the following schema:
create table foo ( test1 int not null auto_increment, test2 int null, primary key (test1) );
Setup a CDBI class like this:
package Foo; use base 'Class::DBI'; __PACKAGE__->columns( All => qw/test1 test2/ ); __PACKAGE__->add_trigger(before_create => \&trigger_code); sub trigger_code { my $self = shift; $self->test2('123') unless $self->test2; }
i.e. we're setting a default value.

Now write a script to insert a row:
Foo->connection('dbi:mysql:dbname=testdb','root',''); Foo->create({});
If all went according to plan, you should have a row inserted into your DB with the 'test2' column set to the default value.

Here comes the weird part. Add the line use Test::MockObject at the top of your script. (You don't actually have to use the module in any way). If you're anything like me, you'll get the error:
Use of uninitialized value in split at /usr/local/share/perl/5.8.4/UNI +VERSAL/can.pm line 51. Called UNIVERSAL::can() as a function, not a method at /usr/local/shar +e/perl/5.8.4/Class/DBI.pm line 265 Can't call method "can" on an undefined value at /usr/local/share/perl +/5.8.4/UNIVERSAL/can.pm line 40.
Extremely strange.... and the sort of thing you can easily spend an afternoon chasing (guess who just did this...)

Now try one of the following: Anyone of these should solve the problem...

Hmm... anyone have any ideas why this might be the case? (Note, I haven't tried this on another db, because SQLite doesn't have auto_increment, and I don't have easy access to anything else that does. I guess it could be a bug with db's that use sequences as well.. or maybe that's nothing to do with it).

Versions used:

In reply to Strange bug involving mysql, auto_increment, Class::DBI and Test::MockObject by Mutant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.