Violation of PRIMARY KEY constraint 'PK_CustCnAttr'

So, I ahve had a lot of errors the past year now, and this one is a new one to me. Anybody have this before?

Error Detail

Message: An error occurred while updating the entries. See the inner exception for details.
Inner Exception Message: Violation of PRIMARY KEY constraint ‘PK_CustCnAttr’. Cannot insert duplicate key in object ‘Erp.CustCnAttr’. The duplicate key value is (VT, 331, 00127, 1, ).
The statement has been terminated.
Program: Epicor.ServiceModel.dll
Method: ShouldRethrowNonRetryableException

Client Stack Trace

at Epicor.ServiceModel.Channels.ImplBase`1.ShouldRethrowNonRetryableException(Exception ex, DataSet[] dataSets)
at Erp.Proxy.BO.CustCntImpl.Update(CustCntDataSet ds)
at Erp.Adapters.CustCntAdapter.OnUpdate()
at Ice.Lib.Framework.EpiBaseAdapter.Update()
at Erp.UI.App.CustomerEntry.Transaction.UpdateContacts()

Inner Exception

Violation of PRIMARY KEY constraint ‘PK_CustCnAttr’. Cannot insert duplicate key in object ‘Erp.CustCnAttr’. The duplicate key value is (VT, 331, 00127, 1, ).
The statement has been terminated.

Inner Stack Trace

Looks like you are trying to create a duplicate attribute

Yeah, thats what I was thinking, but I can’t seem to find the duplicate. This would be a duplicate contact, right?

the key is Company,CustNum,ShipToNum,ConNum,AttrCode

So it is trying to duplicate that combination

1 Like

We’re getting a similar sql job error too.

Violation of PRIMARY KEY constraint ‘pk_Company_ID’. Cannot insert duplicate key in object ‘dbo.SonicOutBound’. The duplicate key value is (001, 834137).

You are trying to create a company with an ID that already exists. What’s context of the issue?

This is the sql job script it is trying to run. I’m not even sure what the script is trying to do but it is erroring out.

use Status
set transaction isolation level read uncommitted
declare @ID as int;
declare @Company as varchar(5);
declare @LineCount as int;
declare @Date as datetime;

set @Date = getdate()

–XXX
set @Company = ‘001’
set @ID = (select MAX(ID) from SonicOutBound)
set @ID = @ID+1
set @LineCount = (select Count(*) from EpicorTest905.dbo.intqueout as E where company = @Company)

IF @LineCount > 0
insert into SonicOutBound(Company, SonicOutBoundCount,ID,Date)
select E.company, Count(*),@ID,@Date from EpicorTest905.dbo.intqueout as E
where company != ‘vn10t’
Group By Company
ELSE
insert into SonicOutBound(Company, SonicOutBoundCount,ID,Date)
values(@Company, ‘0’,@ID,@Date)

Seems like whoever made your SonicOutBound table made Company a primary key - which means you cant have more than one. But the code appears to be trying to make multiple records with same company

Either they need to change the primary key or they intended to update existing records, instead of adding them

If there is a duplicate key, from the (select MAX(ID), can this duplicate key be deleted or make it use the next record max ID?

if the primary key was the ID field, it would work

Not sure what this statement is doing but the where clause might be the issue. Had to modify it.

select E.company, Count(*),@ID,@Date from EpicorTest905.dbo.intqueout as E
where company != ‘vn10t’