problem wit hsequence as primary key

Started by Rob Brown-Baylissover 23 years ago3 messagesgeneral
Jump to latest
#1Rob Brown-Bayliss
rob@zoism.org

Hi, I was insertingrows into a table and had this error:

Cannot insert a duplicate key into unique index stock_transactions_pkey

Now the primary key is inserted by this function:

CREATE FUNCTION "set_primary_key" () RETURNS text AS '
DECLARE
sequence_number text;
location_number text;
retval text;
BEGIN
location_number := to_char(get_loc_key(),''999MI'');
location_number := trim(both '' '' from location_number);
sequence_number := to_char(nextval(''location_seq''),''99999MI'');
sequence_number := trim(both '' '' from sequence_number);
retval := location_number || ''-'' || sequence_number;
RETURN retval;
END;
' LANGUAGE 'plpgsql';

which results in valuse like 12-1234 as a text primary key.

The location_seq is up to 100005 at the moment so how am I getting
duplicate primary keys?

--

*
* Rob Brown-Bayliss

*

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Rob Brown-Bayliss (#1)
Re: problem wit hsequence as primary key

Rob Brown-Bayliss <rob@zoism.org> writes:

sequence_number := to_char(nextval(''location_seq''),''99999MI'');

The location_seq is up to 100005 at the moment so how am I getting
duplicate primary keys?

Time for more 9's in that to_char format, perhaps?

regards, tom lane

#3Rob Brown-Bayliss
rob@zoism.org
In reply to: Tom Lane (#2)
Re: problem wit hsequence as primary key

On Sat, 2002-07-27 at 03:20, Tom Lane wrote:

Rob Brown-Bayliss <rob@zoism.org> writes:

sequence_number := to_char(nextval(''location_seq''),''99999MI'');

The location_seq is up to 100005 at the moment so how am I getting
duplicate primary keys?

Time for more 9's in that to_char format, perhaps?

Yeah :o)

I had a break and a cup o tea and then it was obvious... Thanks

Now a nother question, useinga text based primary on large tables. Will
this slow down queries or does it not really matter?

Thanks

--

*
* Rob Brown-Bayliss
*