How do i get auto incremental key from my sequence? | Community
Skip to main content
Level 2
February 11, 2020
Solved

How do i get auto incremental key from my sequence?

  • February 11, 2020
  • 2 replies
  • 5957 views

I am trying to execute insert operation throught javascript method. 

 

var temp="INSERT INTO gowSampleDB(sSTG_ID,sATTRB_CD,sATTRB_VAL) VALUES ('9383677898998939','GOWTHAMANTEST','GOWTHAMTEST1')";
logInfo("My query :"+temp);
var strSql = sqlExec(temp);

 

 

but its throwing unique constraint exception. How do i refer to my sequence and get the next value to insert?

 

02/11/2020 12:50:57 PM WDB-200001 SQL statement 'INSERT INTO gowSampleDB(sSTG_ID,sATTRB_CD,sATTRB_VAL) VALUES ('9383677898998939','GOWTHAMANTEST','GOWTHAMTEST1')' could not be executed.


02/11/2020 12:50:57 PM PGS-220000 PostgreSQL error: ERROR: duplicate key value violates unique constraint "gowsampledb_id" DETAIL: Key (isampledbid)=(0) already exists.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Milan_Vucetic

Hi @gowthamanrajendran ,

 

if your table has autopk defined as below (check table schema), you do not need to put it in your query, it will be increased automatically.

If you are using your own column as primary key, then database will thrown an error if you try to import value which already exist.

Check as well, if you have unique index on some column.

 

Regards,

Milan

 

2 replies

Milan_Vucetic
Milan_VuceticAccepted solution
Level 9
February 12, 2020

Hi @gowthamanrajendran ,

 

if your table has autopk defined as below (check table schema), you do not need to put it in your query, it will be increased automatically.

If you are using your own column as primary key, then database will thrown an error if you try to import value which already exist.

Check as well, if you have unique index on some column.

 

Regards,

Milan

 

Level 2
February 12, 2020

Hi, I have autopk turned on but still its throwing error, <element autopk="true" label="SampleDB" name="SampleDB" pkSequence="testSchemaSeq">

i dont have my own column as primary key. 

 

 

Level 2
February 12, 2020

Hi,

You can do it in schema, please check in schema, it should be configured like below

<element autopk="true" name="<yourUniqueFieldName>"
pkSequence="<yourSequenceName>" >

 

In javascript, you need to define it like below

var temp="INSERT INTO gowSampleDB(sSTG_ID,sATTRB_CD,sATTRB_VAL) VALUES ( yourSequenceName.NEXTVAL,'GOWTHAMANTEST','GOWTHAMTEST1')";
logInfo("My query :"+temp);
var strSql = sqlExec(temp);

 

Let me know if it works.

 

Regards,

Level 2
February 12, 2020
No i tried this. this is throwing another exception. missing from clause ....