Unique Contraint Critical Error // 1062 Duplicate entry - Help

Your software
My Mautic version is: 4.4.4
My PHP version is: 7.4

Your problem
My problem is:

Using the tracking script we are seeing a 500 error when trying to associate anonymous user to known user and give tags to that user

These errors are showing in the log:

[2023-05-03 07:07:05] mautic.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\UniqueConstraintViolationException: "An exception occurred while executing 'INSERT INTO lead_tags_xref (lead_id, tag_id) VALUES (?, ?)' with params ["962459", 77]:  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '962459-77' for key 'PRIMARY'" at /var/www/mautic/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 74 {"exception":"[object] (Doctrine\\DBAL\\Exception\\UniqueConstraintViolationException(code: 0): An exception occurred while executing 'INSERT INTO lead_tags_xref (lead_id, tag_id) VALUES (?, ?)' with params [\"962459\", 77]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '962459-77' for key 'PRIMARY' at /var/www/mautic/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:74, Doctrine\\DBAL\\Driver\\PDO\\Exception(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '962459-77' for key 'PRIMARY' at /var/www/mautic/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '962459-77' for key 'PRIMARY' at /var/www/ams/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117)"} {"hostname":"my-hostname","pid":1894640}

Is this maybe because of the INSERT into lead_tags and could it be fixed with changing this to an update?
Just shooting in the dark here, so any direction is gladly welcomed

Hi, no this means that given contact with id 962459 is already associated with tag that has id 77 and so mysql server does not allow an insert with the same value.

The combination of lead_id and tag_id can only occur once in the table.

This seems to be a re-occrung problem . is there a way around this ?

When I am trying to send a simple email it is throwing this error.

You are probably doing this within campaign and somehow manipulating the tags of the contact.

I do not think email sending as such does anything connected to tags.

This is really not a solution, but you could try to remove the FK constraint on lead_tags table.

Anyway, I am inerrested in resolving this the right way - which means figuring out why/where you are trying to insert duplicated combinations of the keys.