0

Given that I have the following create syntax for postgres

CREATE TABLE CusGoals(
  biRecipientId BIGINT Default 0, 
  iEnvironment SMALLINT Default Null, 
  iGoal01 SMALLINT Default Null, 
  iGoal02 SMALLINT Default Null, 
  iGoal03 SMALLINT Default Null, 
  iGoal04 SMALLINT Default Null, 
  iGoal05 SMALLINT Default Null, 
  iGoal06 SMALLINT Default Null, 
  iGoal07 SMALLINT Default Null, 
  iGoal08 SMALLINT Default Null, 
  iGoal09 SMALLINT Default Null, 
  iGoal10 SMALLINT Default Null, 
  iGoal11 SMALLINT Default Null, 
  iGoal12 SMALLINT Default Null, 
  iGoal13 SMALLINT Default Null, 
  iGoal14 SMALLINT Default Null, 
  iGoal15 SMALLINT Default Null, 
  iGoal16 SMALLINT Default Null, 
  iGoal17 SMALLINT Default Null, 
  iGoalsId INTEGER Default 0, 
  iHealthWellness SMALLINT Default Null, 
  iInclusion SMALLINT Default Null, 
  iPeople SMALLINT Default Null, 
  iPlanet SMALLINT Default Null, 
  iResponsibleConsumption SMALLINT Default Null, 
  iSustainableInfrastructure SMALLINT Default Null, 
  tsCreated TIMESTAMPTZ, 
  tsLastModified TIMESTAMPTZ
);

CREATE UNIQUE INDEX CusGoals_id ON CusGoals(iGoalsId);      
INSERT INTO CusGoals (iGoalsId) VALUES (0);  

-- Log: Creating sequence 'auto_cusgoals_seq' for 'cus:goals'.

SELECT CreateSequenceIfNecessary('auto_cusgoals_seq', '1000', 'cache 30');

If I have the following insert statement

INSERT INTO CusGoals (iGoal01,iGoal04,iGoal14,iPlanet,iPeople,iInclusion,iResponsibleConsumption) VALUES (2,3,4,44,56,5,4)

How can I use the auto_cusgoals_seq on the iGoalsId to update correctly as I am getting the following error.

PGS-220000 PostgreSQL error: ERROR: duplicate key value violates unique constraint "cusgoals_id" DETAIL: Key (igoalsid)=(0) already exists. WDB-200001 SQL statement 'INSERT INTO CusGoals (iGoal01,iGoal04,iGoal14,iPlanet,iPeople,iInclusion,iResponsibleConsumption) VALUES (2,3,4,44,56,5,4)' could not be executed.

2 Answers 2

1

The new sequence must be bumped to the current max value.

You can reset it using

SELECT setval('auto_cusgoals_seq', max(igoalsid)) FROM CusGoals;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i got it working like this also; ` var sqlSyntax = "INSERT INTO CusGoals (igoalsid, "+stringColumns+") VALUES (nextval('auto_cusgoals_seq'),"+stringValues+")";`
1

Assuming that biRecipientId is your primary key, just alter the data format from bigint to serial.

https://www.postgresqltutorial.com/postgresql-serial/

1 Comment

my application does not allow such configuration on the schema creation and I dont have access to the db directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.