currval = currval+1

From: salah jubeh <s_jubeh(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: currval = currval+1
Date: 2011-06-01 14:27:57
Message-ID: 895934.61056.qm@web161508.mail.bf1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have the following SQL statements

BEGIN;
-- account_id is a sequence
INSERT INTO account (name) VALUES ('test customer'||random()::text);
-- account_id is a foreign key
INSERT INTO account_detail (account_id,......) VALUES ((SELECT * from
currval('account_acccount_id_seq')), ........);
COMMIT;

ERROR: insert or update on table "account_detail" violates foreign key
constraint ...

I have executed the above without a transaction but in the same session, and the
issue was that the current value of the account points to the curval + 1

I have solved the above by subtracting 1 in the second insert statement. Why I
am having this problem, I have used current value many time and this is the
first time I have this behavior.

Regards

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-06-01 14:54:36 Re: currval = currval+1
Previous Message Tom Lane 2011-06-01 14:11:11 Re: Mixed up protocol packets in server response?