Table with "Identity column" as PK returns Duplicate key after adding a column.

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
pkoopmanpk
Posts: 49
Joined: Wed Apr 26, 2017 7:12 pm

Table with "Identity column" as PK returns Duplicate key after adding a column.

Post by pkoopmanpk »

I'm experimenting with the "Identity column" field. The generated column looks like this:

Code: Select all

CREATE TABLE DEVLIB/VBSTRG ( 
	TRGID BIGINT GENERATED ALWAYS AS IDENTITY ( 
	START WITH 1 INCREMENT BY 1 
	NO MINVALUE NO MAXVALUE 
	NO CYCLE NO ORDER 
	CACHE 20 ) 
. . . 
I had a table with some data which needed an extra column.

After adding the field and checking it into the IBM i, every insert kept returning a IO$STS = VE. Joblogs said there was a duplicate key.

Only after clearing data with a CLRPFM, the table started accepting new records.

Am I missing something? Or is clearing the table the only way in these cases?
pkoopmanpk
Posts: 49
Joined: Wed Apr 26, 2017 7:12 pm

Re: Table with "Identity column" as PK returns Duplicate key after adding a column.

Post by pkoopmanpk »

A quick search on CPYF and identity columns returns this tip.
1. Lock the table
2. Query, selecting the max-value of the identity column
3. Alter the table, using the identity column reset clause to set the next value the max-value plus one
4. Unlock the table
Could it be that Lansa forgets to do this when checking in an existing table?
appbuilder
Posts: 28
Joined: Sat Jan 16, 2016 9:53 am

Re: Table with "Identity column" as PK returns Duplicate key after adding a column.

Post by appbuilder »

You may need to reset the identity column value after adding a column.

Find the last identity column value and use
ALTER TABLE TEST ALTER TEST_ID RESTART WITH X;
where X is the next value.
Last edited by appbuilder on Tue May 14, 2024 4:19 am, edited 1 time in total.
Thanks,

Don
pkoopmanpk
Posts: 49
Joined: Wed Apr 26, 2017 7:12 pm

Re: Table with "Identity column" as PK returns Duplicate key after adding a column.

Post by pkoopmanpk »

Hi Don,

After some further Googling, I agree. Thank you for the pointer in the right direction. :)

I think Lansa should do this automatically. But until they fix it, this is a good work around.

Best regards, Peter
pkoopmanpk
Posts: 49
Joined: Wed Apr 26, 2017 7:12 pm

Re: Table with "Identity column" as PK returns Duplicate key after adding a column.

Post by pkoopmanpk »

I use this simple script now to restart the identity column:

Code: Select all

begin
 for V as CUR cursor for 
    select 
       'ALTER TABLE VBSTRG ALTER TRGID RESTART WITH ' || trim(char(max(TRGID) + 1)) as STMT
    from 
        VBSTRG
  do 
    execute immediate V.STMT;
  end for;
end;
Post Reply