I have just been getting the same error. My table only has 1 row, and there is not duplicate.
TLDR: check your foreign keys, make sure the value exists in the parent table. MySQL 5.6.3 apparently can't tell you the real reason for the error.
mysql> select * from users_sessions;
+---------+----------------------------+---------------------+
| user_id | session_id | last_accessed |
+---------+----------------------------+---------------------+
| 3 | 6n02k8catt2kdn30b92ljtrpc6 | 2019-01-13 23:30:53 |
+---------+----------------------------+---------------------+
1 row in set (0.00 sec)
mysql>
mysql> INSERT INTO `users_sessions` VALUES(3,"fbfibdog1qumlj5mg4kstbagu7","2019-01-14 18:37:15") ON DUPLICATE KEY UPDATE last_accessed = "2019-01-14 18:37:15";
ERROR 1062 (23000): Duplicate entry '3-fbfibdog1qumlj5mg4kstbagu7' for key 'PRIMARY'
There is no such duplicate key!
I tried running the same insert without the ON DUPLICATE KEY ...
clause, and got a better clue of what was happening.
mysql> INSERT INTO `users_sessions` VALUES(3,"fbfibdog1qumlj5mg4kstbagu7","2019-01-14 18:37:15");
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`login`.`users_sessions`, CONSTRAINT `fk_sessions_id` FOREIGN KEY (`session_id`) REFERENCES `sessions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Now we're onto something. So I tried running the original query with a different value that does have a matching value in the foreign key table. The query ran without issue.
Why does MySQL seem to issue the wrong error? I'm not sure, but it probably has to do with the way ON DUPLICATE KEY UPDATE
is implemented. I tried running the same insert query as a REPLACE INTO
query and got the same error.
mysql> REPLACE INTO `users_sessions` VALUES(3,"fbfibdog1qumlj5mg4kstbagu7","2019-01-14 18:37:15");
ERROR 1062 (23000): Duplicate entry '3-fbfibdog1qumlj5mg4kstbagu7' for key 'PRIMARY'
If you run into this error for no apparent reason, check your foreign keys. Try a regular INSERT
query (instead of REPLACE INTO
or INSERT ... ON DUPLICATE KEY UPDATE...
) as well.