Re: [HACKERS] create table and default 'now' problem ?

From: The Hermit Hacker <scrappy(at)hub(dot)org>
To: Mike Mascari <mascarim(at)yahoo(dot)com>
Cc: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] create table and default 'now' problem ?
Date: 1999-09-21 06:01:58
Message-ID: Pine.BSF.4.10.9909210301321.38923-100000@thelab.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 20 Sep 1999, Mike Mascari wrote:

> One way around this bug is to create a SQL function
> which returns now() and use it as the default value:
>
> 1. create function mynow() returns datetime
> as 'SELECT now()::datetime' LANGUAGE 'SQL';
>
> 2. create table test (a datetime default mynow(), b
> int4);
>
> Now things should work:
>
> insert into test (b) values (1);
> insert into test (b) values (2);
>
> select * from test;
> a |b
> ----------------------------+-
> Tue Sep 21 01:05:02 1999 EDT|1
> Tue Sep 21 01:05:08 1999 EDT|2
> (2 rows)
>
> Hope this helps,

Why the 'create function'?

hardware=> create table test_table ( a int4, ts datetime default 'now' );
CREATE
hardware=> insert into test_table values ( 1 ) ;
INSERT 115445 1
hardware=> select * from test_table;
a|ts
-+----------------------------
1|Tue Sep 21 02:00:50 1999 EDT
(1 row)

>
> Mike Mascari
> (mascarim(at)yahoo(dot)com)
>
> --- Oleg Bartunov <oleg(at)sai(dot)msu(dot)su> wrote:
> > Hi,
> >
> > how I could create table with datetime field default
> > to 'now'::text in
> > a way Jan did in his shoes rule example ?
> >
> > If I do:
> > test=> create table test ( a datetime default 'now',
> > b int4);
> > CREATE
> > test=> insert into test (b) values (1);
> > INSERT 1677899 1
> > test=> insert into test (b) values (2);
> > INSERT 1677900 1
> > test=> select * from test;
> > a |b
> > ----------------------------+-
> > Tue 21 Sep 01:48:27 1999 MSD|1
> > Tue 21 Sep 01:48:27 1999 MSD|2
> > (2 rows)
> >
> > I always get datetime of the moment I created the
> > table, but I'd like
> > to have datetime of moment I insert.
> >
> > Regards,
> >
> > Oleg
> >
> __________________________________________________
> Do You Yahoo!?
> Bid and sell for free at http://auctions.yahoo.com
>
> ************
>

Marc G. Fournier ICQ#7615664 IRC Nick: Scrappy
Systems Administrator @ hub.org
primary: scrappy(at)hub(dot)org secondary: scrappy(at){freebsd|postgresql}.org

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message The Hermit Hacker 1999-09-21 06:03:58 Re: [HACKERS] create table and default 'now' problem ?
Previous Message Thomas Lockhart 1999-09-21 05:59:54 Re: [HACKERS] Re: HISTORY for 6.5.2