(1062, "Duplicate entry '0' for key 'PRIMARY'")

17,109

Could you check in your mysql database what the incrementation number is at? It seems that mysql is trying to insert a row with the same id.

Share:
17,109
maahd
Author by

maahd

Updated on June 04, 2022

Comments

  • maahd
    maahd almost 2 years

    I'm getting this error (1062, "Duplicate entry '0' for key 'PRIMARY'"). This happened after I migrated my Django app from sqlite3 to MySQL. Here is the concerned table:

    mysql> describe meddy1_specialization;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(30) | NO   |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    2 rows in set (0.01 sec)
    

    Here is the model:

    class Specialization(models.Model):
        name = models.CharField(max_length=30)
    
        def __unicode__(self):
            return self.name
    

    Here is the data in the table:

    mysql> select * from meddy1_specialization;
    +----+--------------------+
    | id | name               |
    +----+--------------------+
    |  1 | Dentist            |
    |  2 | Dermatologist      |
    |  3 | Primary Care       |
    |  4 | Ophthalmologist    |
    |  5 | Pediatrician       |
    |  6 | Orthopedist        |
    |  7 | Ear, Nose & Throat |
    |  8 | Gynecologist       |
    |  9 | test               |
    | 13 | test               |
    | 14 | test1              |
    +----+--------------------+
    11 rows in set (0.00 sec)
    
  • maahd
    maahd almost 10 years
    Yes I checked that and told MySQL to increment from where the 'id' left off...14 in the example above. Didn't work. Then I deleted the 'id' column and re added. Didn't work. I used truncate to delete table with all data and recreate. Didn't work...
  • Eagllus
    Eagllus almost 10 years
    If you try to insert a row through MySQL self. Does that work or does that returns the error?