This should fix #4836 but is not tested

I have not been able to test this (as the issue occurs intermittently), but this should fix the issue. I guess the true and correct way of fixing this would be to find out why this occurs in the first place, but the "ON DUPLICATE KEY UPDATE" should fix the issue regardless.