Migration failed: Cannot insert duplicate key row in object 'dbo.PackageDefinitions' with unique index 'UX_PackageDefinitions_FeedId_NormalizedId_NormalizedVersion'

How to solve the error thrown during upgrade: "Migration failed: Cannot insert duplicate key row in object 'dbo.PackageDefinitions' with unique index 'UX_PackageDefinitions_FeedId_NormalizedId_NormalizedVersion'. The duplicate key value is ('KEYVALUE', PACKAGENAME, PACKAGEVERSION). The statement has been terminated. "?

During upgrade from 19.10.14 version that has the NuGet.Repository.Type as Legacy, the installer will migrate all packages information to database too (starting with 2020.10.x version NuGet.Repository.Type will be only Composite) and in some situations the following error is thrown:

Calling custom action UiPath.OrchestratorCAs!UiPath.OrchestratorCAs.CustomActions.Deferred.ImportMigratePackagesAction.ImportMigratePackages
Migration failed: Cannot insert duplicate key row in object 'dbo.PackageDefinitions' with unique index 'UX_PackageDefinitions_FeedId_NormalizedId_NormalizedVersion'. The duplicate key value is (KEYVALUE', PACKAGENAME, PACKAGEVERSION).
The statement has been terminated.
System.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'dbo.PackageDefinitions' with unique index 'UX_PackageDefinitions_FeedId_NormalizedId_NormalizedVersion'. The duplicate key value is KEYVALUE', PACKAGENAME, PACKAGEVERSION).
The statement has been terminated


When the metadata is created during the upgrade it queries the Name and Version of the package from the Nuspec files, if two Nuspec files have the same content it will considering them as duplicates.

Resolution:

  1. Delete one of the duplicated packages
  2. Edit one of the PackageName.nuspec file and change the version of it in the Version node

For example:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
<metadata>
<id>UiPath.FlexiCapture.Activities</id>
<version>0.8.6701.27340-alpha</version>
<title>UiPath.FlexiCapture.Activities</title>
<authors>UiPath</authors>
<owners>UiPath</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<projectUrl>https://activities.uipath.com/docs/about-the-flexicapture-activities-pack</projectUrl>
<iconUrl>http://www.uipath.com/favicon.ico</iconUrl>
<description>FlexiCapture Activities Pack</description>
<dependencies />
</metadata>
</package>