How to resolve the error "Cannot insert duplicate key row in object resx folders with unique index IX Folders AccountKey TenantKey Code"

Error: Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'resx.Folders' with unique index 'IX_Folders_AccountKey_TenantKey_Code'.

Error:

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'resx.Folders' with unique index 'IX_Folders_AccountKey_TenantKey_Code'. The duplicate key value is (74738cf1-f94f-4d00-87ed-e0ac90ab652a, 74738cf1-f94f-4d00-87ed-e0ac90ab652a, 0000000000.0000000002). The statement has been terminated.


Resolution:

Note:
Ensure proper backups are taken before making any database changes.

  1. Execute the below commands
select * from [dbo].[OrganizationUnits] ou where ou.TenantId = @tenantId and ou.Code = '0000000000.0000000001'

select @folderKey

select @latestLedger

select * from [dbo].[Ledger] l where JSON_VALUE(l.CustomData, '$.folderKey') = @folderKey and l.[LedgerId] > @latestLedger

  1. Modify the account and tenant key as per the Orchestrator details.
delete from [resx].[Folders]

where AccountKey = '74738cf1-f94f-4d00-87ed-e0ac90ab652a'

and TenantKey = '74738cf1-f94f-4d00-87ed-e0ac90ab652a'

and Code = '0000000000.0000000001'