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. "?

Issue Description:
While upgrading from a version that has a Legacy Nuget Repository type, the installer will migrate all packages information to the database too and in some situations, the following error is thrown:

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 full stack trace is depicted below:

image.png

Read more about Nuget package repositories here .


Root Cause:
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 consider them as duplicates.


Resolution:
Delete one of the duplicated packages.
Or
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"?>

  
    UiPath.FlexiCapture.Activities
    0.8.6701.27340-alpha
    UiPath.FlexiCapture.Activities
    UiPath
    UiPath
    false
    https://activities.uipath.com/docs/about-the-flexicapture-activities-pack
    http://www.uipath.com/favicon.ico
    FlexiCapture Activities Pack