bpphillips has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I'm making my first foray into an application based on Class::DBI with MySQL as the backend DB. I'm loving everything about how easy C::DBI makes things through defining relationships, triggers, constraints ad naseum! :-) I've also been using DateTime::Format::MySQL to auto-inflate my date/datetime/timestamp columns into DateTime objects. One problem I've run into -- and the reason I'm posting this here -- is dealing with MySQL's special "NULL" datetime (represented as 0000-00-00 00:00:00). DateTime understandably refuses to accept this as a valid date.

Using SuperSearch, I came across one node here at the monestary (Re: Expand Class::DBI Field to DateTime Object) about this issue but the work around didn't seem very satisfactory (store the date values in another table and use a might_have relationship to inflate the to DateTime objects).

I've tried checking the value in my inflate method and returning undef if the date is the '0000-00-00 00:00:00' value. This seems to cause C::DBI to pass that undef value to DateTime->new() which obviously fails. I know I could just default it to DateTime->now() or maybe even DateTime->from_epoch(epoch=>0) but I'd rather be able to keep the idea of a "NULL" date somehow. Should I just subclass DateTime with my own object that can handle this special value?

I'm hoping someone else has come up against this issue before and can give me some advice on how to approach this issue.

Thanks in advance -- Brian

Replies are listed 'Best First'.
Re: Class::DBI - MySQL - DateTime
by perrin (Chancellor) on Jun 07, 2005 at 14:35 UTC
    MySQL doesn't store NULL as '0000-00-00 00:00:00' -- it stores it as NULL. Take the NOT NULL constraint off that column so you ca have actual NULLs in it and then everything will work.
      That's an excellent point -- and one I didn't think about. It does appear to work as I need it to. Thanks!