Why am I getting a duplicate key error in my test suite?

I started noticing this error while running my test suite. I can reproduce it by going into the temporary tenant created during the test run.

In this specific case, I have logic that runs on a workflow transition, which inserts Directory records. I initially thought the issue might be there, since the Autonumber Definition for Directory is based on ContactID.

However, I’m not actually passing ContactID when inserting Directory records, which made it confusing why the duplicate key error was happening in the first place?

Error Message: "AutomationError: The current transaction was aborted due to a prior failed database transaction. Most likely caused by: ERROR: duplicate key value violates unique constraint "kcuweltlq_a" Detail: Key (nwtenantstripe, nwnateid, ((nwdata ->> 'ContactID'::text)::double precision))=(stx_rpdevautoteststx_t192, 00000000-0000-0000-0000-000000000000, 1) already exists. Caused by: ERROR: current transaction is aborted, commands ignored until end of transaction block.

The issue turned out to be related to Automatic Numbering and seed data. My temporary tenant already included Directory records (inherited from the seed environment), but there were no corresponding AutomaticNumberValues records for Directory to indicate the next number to use.

As a result, when new Directory records were inserted, the numbering started back at 1 (as defined in the Automatic Number Definition for Directory). Since a record with ContactID = 1 already existed, this caused a duplicate key error.

The fix was to include AutomaticNumberValues table in the Temporary Tenant Template Setup. This ensures that when the temporary tenant is created, it carries over the correct next-number values, preventing the numbering from restarting at 1 and avoiding conflicts.

After adding AutomaticNumberValues, the duplicate key error no longer occurred in my test suite runs.

Thank you @alec.ruth and @amruta.varshini for helping me resolve this!

3 Likes