(1062,“重复输入'0'用于键'PRIMARY'”)

问题描述 投票:5回答:2

我收到此错误(1062, "Duplicate entry '0' for key 'PRIMARY'")。这是在我将我的Django应用程序从sqlite3迁移到MySQL之后发生的。这是关注的表:

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)

这是模型:

class Specialization(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

这是表中的数据:

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)
mysql django duplicates primary-key auto-increment
2个回答
1
投票

你能在你的mysql数据库中检查增量数是多少吗?看来mysql试图插入一个具有相同id的行。


0
投票

在尝试迁移之前,请确保target_table为空...首先从目标表中删除所有内容。错误可能是由id号的重复引起的,即使它是= 0(新记录的默认值)。尝试明确定义每条记录的每个id号。

在您的情况下,从sqlite3导出到script.sql文件,然后找到数据并将其添加到脚本中;

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表到另一个Msql表的迁移信息时......可能还需要显式定义...可以在源表上添加具有唯一ID的列并自动增加,这样可以使迁移更容易使用该栏目......

© www.soinside.com 2019 - 2024. All rights reserved.