Skip to main content
added 187 characters in body
Source Link

You can just use this script here https://takeoff.digital/2022/09/15/duplicate-primary-key-error-in-magento-2/
Please read the explanations in the above url if it doesn't make sense for you as this will generate a couple of text files which you will then have to execute in your mysql editor.

<?php
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "yourdb";
$deletefile = "path/del.txt";
$insfile = "path/ins.txt";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT url_rewrite_id  FROM database_name.catalog_url_rewrite_product_category group by url_rewrite_id having count(*) > 1 order by url_rewrite_id desc;";
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
    while ($row = $result->fetch_assoc())
    {
        $roid = $row["url_rewrite_id"];
        $line = "DELETE FROM database_name.catalog_url_rewrite_product_category where url_rewrite_id = '$roid';" . "\n";
        file_put_contents($deletefile, $line, FILE_APPEND | LOCK_EX);
        $query2 = "SELECT * FROM database_name.catalog_url_rewrite_product_category WHERE url_rewrite_id = '$roid' limit 1;" . "\n";
        $data = $conn->query($query2)->fetch_assoc();
        $urid = $data["url_rewrite_id"];
        $catid = $data["category_id"];
        $pid = $data["product_id"];
        $insquery = "INSERT INTO database_name.catalog_url_rewrite_product_category (url_rewrite_id, category_id, product_id) VALUES('$urid', '$catid','$pid');" . "\n";
        file_put_contents($insfile, $insquery, FILE_APPEND | LOCK_EX);
    }
}
$conn->close();
?>

You can just use this script here https://takeoff.digital/2022/09/15/duplicate-primary-key-error-in-magento-2/

<?php
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "yourdb";
$deletefile = "path/del.txt";
$insfile = "path/ins.txt";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT url_rewrite_id  FROM database_name.catalog_url_rewrite_product_category group by url_rewrite_id having count(*) > 1 order by url_rewrite_id desc;";
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
    while ($row = $result->fetch_assoc())
    {
        $roid = $row["url_rewrite_id"];
        $line = "DELETE FROM database_name.catalog_url_rewrite_product_category where url_rewrite_id = '$roid';" . "\n";
        file_put_contents($deletefile, $line, FILE_APPEND | LOCK_EX);
        $query2 = "SELECT * FROM database_name.catalog_url_rewrite_product_category WHERE url_rewrite_id = '$roid' limit 1;" . "\n";
        $data = $conn->query($query2)->fetch_assoc();
        $urid = $data["url_rewrite_id"];
        $catid = $data["category_id"];
        $pid = $data["product_id"];
        $insquery = "INSERT INTO database_name.catalog_url_rewrite_product_category (url_rewrite_id, category_id, product_id) VALUES('$urid', '$catid','$pid');" . "\n";
        file_put_contents($insfile, $insquery, FILE_APPEND | LOCK_EX);
    }
}
$conn->close();
?>

You can just use this script here https://takeoff.digital/2022/09/15/duplicate-primary-key-error-in-magento-2/
Please read the explanations in the above url if it doesn't make sense for you as this will generate a couple of text files which you will then have to execute in your mysql editor.

<?php
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "yourdb";
$deletefile = "path/del.txt";
$insfile = "path/ins.txt";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT url_rewrite_id  FROM database_name.catalog_url_rewrite_product_category group by url_rewrite_id having count(*) > 1 order by url_rewrite_id desc;";
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
    while ($row = $result->fetch_assoc())
    {
        $roid = $row["url_rewrite_id"];
        $line = "DELETE FROM database_name.catalog_url_rewrite_product_category where url_rewrite_id = '$roid';" . "\n";
        file_put_contents($deletefile, $line, FILE_APPEND | LOCK_EX);
        $query2 = "SELECT * FROM database_name.catalog_url_rewrite_product_category WHERE url_rewrite_id = '$roid' limit 1;" . "\n";
        $data = $conn->query($query2)->fetch_assoc();
        $urid = $data["url_rewrite_id"];
        $catid = $data["category_id"];
        $pid = $data["product_id"];
        $insquery = "INSERT INTO database_name.catalog_url_rewrite_product_category (url_rewrite_id, category_id, product_id) VALUES('$urid', '$catid','$pid');" . "\n";
        file_put_contents($insfile, $insquery, FILE_APPEND | LOCK_EX);
    }
}
$conn->close();
?>
Source Link

You can just use this script here https://takeoff.digital/2022/09/15/duplicate-primary-key-error-in-magento-2/

<?php
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "yourdb";
$deletefile = "path/del.txt";
$insfile = "path/ins.txt";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT url_rewrite_id  FROM database_name.catalog_url_rewrite_product_category group by url_rewrite_id having count(*) > 1 order by url_rewrite_id desc;";
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
    while ($row = $result->fetch_assoc())
    {
        $roid = $row["url_rewrite_id"];
        $line = "DELETE FROM database_name.catalog_url_rewrite_product_category where url_rewrite_id = '$roid';" . "\n";
        file_put_contents($deletefile, $line, FILE_APPEND | LOCK_EX);
        $query2 = "SELECT * FROM database_name.catalog_url_rewrite_product_category WHERE url_rewrite_id = '$roid' limit 1;" . "\n";
        $data = $conn->query($query2)->fetch_assoc();
        $urid = $data["url_rewrite_id"];
        $catid = $data["category_id"];
        $pid = $data["product_id"];
        $insquery = "INSERT INTO database_name.catalog_url_rewrite_product_category (url_rewrite_id, category_id, product_id) VALUES('$urid', '$catid','$pid');" . "\n";
        file_put_contents($insfile, $insquery, FILE_APPEND | LOCK_EX);
    }
}
$conn->close();
?>