FAQ
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.

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?

Mark

Search Discussions

  • Matt S Trout at Apr 10, 2006 at 8:58 pm

    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()

    ?
    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;
    $obj->discard_changes;

    might work (I have an awful cold today so I may be off there).

    --
    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/ +
  • Mark Hedges at Apr 10, 2006 at 9:15 pm

    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
  • Matt S Trout at Apr 11, 2006 at 1:46 pm

    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/ +

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbix-class @
categoriesperl, catalyst
postedApr 10, '06 at 8:51p
activeApr 11, '06 at 1:46p
posts4
users2
websitedbix-class.org
irc#dbix-class

2 users in discussion

Matt S Trout: 2 posts Mark Hedges: 2 posts

People

Translate

site design / logo © 2022 Grokbase