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:
我收到此错误(1062,“重复输入'0'用于键'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)
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)
2 个解决方案
#1
1
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.
你能在你的mysql数据库中检查增量数是多少吗?看来mysql试图插入一个具有相同id的行。
#2
0
Make sure the target_table is empty before you try the migration... delete all from the target table first. The error may be caused by duplication of an id number, even if it is = 0 (default for new record). Try to explicitly define each of the id numbers for each record.
在尝试迁移之前,请确保target_table为空...首先从目标表中删除所有内容。错误可能是由id号的重复引起的,即使它是= 0(新记录的默认值)。尝试明确定义每条记录的每个ID号。
In your case export from sqlite3 to a script.sql file, then find the data and add it to a script like;
在您的情况下,从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
Another scenario where this happens is when migration information from one Msql table to another... again explicit definitions might be required... one could add a column on the source table with the unique id and auto-increment this will then make migrating easier using that column...
发生这种情况的另一种情况是当从一个Msql表到另一个Msql表的迁移信息时......可能还需要显式定义...可以在源表上添加具有唯一ID的列并自动增加,这将使迁移更容易使用该栏目......
#1
1
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.
你能在你的mysql数据库中检查增量数是多少吗?看来mysql试图插入一个具有相同id的行。
#2
0
Make sure the target_table is empty before you try the migration... delete all from the target table first. The error may be caused by duplication of an id number, even if it is = 0 (default for new record). Try to explicitly define each of the id numbers for each record.
在尝试迁移之前,请确保target_table为空...首先从目标表中删除所有内容。错误可能是由id号的重复引起的,即使它是= 0(新记录的默认值)。尝试明确定义每条记录的每个ID号。
In your case export from sqlite3 to a script.sql file, then find the data and add it to a script like;
在您的情况下,从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
Another scenario where this happens is when migration information from one Msql table to another... again explicit definitions might be required... one could add a column on the source table with the unique id and auto-increment this will then make migrating easier using that column...
发生这种情况的另一种情况是当从一个Msql表到另一个Msql表的迁移信息时......可能还需要显式定义...可以在源表上添加具有唯一ID的列并自动增加,这将使迁移更容易使用该栏目......