[Dbix-class] setting SQL function

Mark Hedges hedges at ucsd.edu
Mon Apr 10 23:15:28 CEST 2006



On Mon, 10 Apr 2006, Matt S Trout wrote:
> Mark Hedges wrote:
> > Sorry I've missed how to do this.  I want to set columns to raw 
> > SQL.  For instance, I want to set a datetime field to sql 
> > 'now()' (i.e. 'CURRENT_TIMESTAMP()').  
> > 
> > In mysql type 'timestamp' fields do the right thing if you set 
> > the value to undef, but type 'datetime' fields do not, and you 
> > cannot set a default value to CURRENT_TIMESTAMP for them like 
> > you can for type 'timestamp' fields.
> 
> timestamp DEFAULT now() ?

Yeah for 'timestamp' that works, but only for the first one in a 
table, you can only have one.  But for a 'datetime' field type, 
you can't use a time function as a default.  MySQL is wacky.

> > I could always create a DateTime->now() object and plug it into 
> > the field when I create it (or do this as a default value in an 
> > overloaded 'new()' subroutine for the class.  But this seems 
> > less efficient than (and slightly asynchronous from) SQL now().
> > Or I could make this a timestamp column.  But setting direct SQL 
> > in $row->new({ }) could be useful in other ways.  Possible?
> 
> my $obj = $rs->new_result({ created => \'now()' });
> 
> $obj->insert;

Cool.

   my $obj = ObjClass->new({ created => \'now()' }) 

does indeed work.  Thanks for the hint.

(I guess it doesn't make as much sense to me to create a 
ResultSet when all I want is to create a single result --- I 
guess I like working directly with classes better.  Same effect.)

Thanks!

Mark




More information about the Dbix-class mailing list