![]() ![]() clause, and got a better clue of what was happening. ![]() I tried running the same insert without the ON DUPLICATE KEY. Mysql> INSERT INTO `users_sessions` VALUES(3,"fbfibdog1qumlj5mg4kstbagu7"," 18:37:15") ON DUPLICATE KEY UPDATE last_accessed = " 18:37:15" ĮRROR 1062 (23000): Duplicate entry '3-fbfibdog1qumlj5mg4kstbagu7' for key 'PRIMARY' ![]() MySQL 5.6.3 apparently can't tell you the real reason for the error. TLDR: check your foreign keys, make sure the value exists in the parent table. My table only has 1 row, and there is not duplicate. Be careful with any of these options if cross database compatibility is (or is likely to be in future) one of your goals. MS SQL Server, Oracle and some other systems support MERGE statements as defined in the SQL:2003 standard (see (SQL)) which is intended to achieve the same functionality and more. There is also the mysql specific ON DUPLICATE KEY UPDATE option (see ) if you are happy to sacrifice compatibility with other RDBMSs.Īs of version 9.5 Postgres supports a similar feature with slightly different syntax ( ON CONFLICT DO UPDATE/NOTHING - see ). There are a couple of ways around it: check before you insert and either do an update (if something might have changed) or just don't do anything. ![]() If you try INSERT a duplicate values for a primary key (or a unique index) you will always get that error. You will find one matching row, and that your code at the point of the error is trying to insert a second. It isn't saying that there is a duplicate entry in the table already, it is saying that there is already one entry in there with that value for the primary key and it is refusing to insert a second for that reason. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |