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

I get this error (1062, "Duplicate entry '0' for key 'PRIMARY'"). This happened after I migrated my Django application from sqlite3 to MySQL. Here is the corresponding 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 are 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)
+4
source share
2 answers

Can you check your mysql database which contains the increment number? It seems like mysql is trying to insert a row with the same id.

0
source

, target_table , ... . , 0 ( ). id .

sqlite3 script.sql, script ;

insert into `meddy1_specialization` (`id`, `name`) VALUES 
(1 'Dentist'),
(2,'Dermatologist'),
(3,'Primary Care'),
(4,'Ophthalmologist'),
(5,'Pediatrician'),
...--plug in all your values in this fashion

, , - Msql ... ... , ...

0

Source: https://habr.com/ru/post/1548361/


All Articles