[Dbix-class] setting SQL function

Matt S Trout dbix-class at trout.me.uk
Tue Apr 11 15:46:16 CEST 2006


Mark Hedges wrote:
> 
> 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.)

Not the same effect at all. If you ever need to have two copies of your schema 
against different databases calling the class method isn't going to work.

This may or may not be considered a problem though.

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list