Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django &引用;“唯一约束失败”;迁移时_Django - Fatal编程技术网

Django &引用;“唯一约束失败”;迁移时

Django &引用;“唯一约束失败”;迁移时,django,Django,错误消息: Operations to perform: Apply all migrations: admin, auth, contenttypes, contest1, dashboard, sessions, tiaozhancup Running migrations: Applying dashboard.0012_auto_20200104_1633...Traceback (most recent call last): File "C:\Users\acer\Des

错误消息:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, contest1, dashboard, sessions, tiaozhancup
Running migrations:
  Applying dashboard.0012_auto_20200104_1633...Traceback (most recent call last):
  File "C:\Users\acer\Desktop\来了喔\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\acer\Desktop\来了喔\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: new__dashboard_teacher.TID
models.py

class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    username = models.CharField(max_length=50, blank=True, null=True) #这个是为了能在admin能用,就当他是真实姓名吧,管它呢
    email = models.EmailField(max_length=254, blank=True, null=True)
    phone_number = models.CharField(max_length=50, blank=True, null=True)
    SID = models.CharField(max_length=50, blank=True, null=True, unique=True,) 
    gender = models.CharField(max_length=50, blank=True, null=True)
    school = models.CharField(max_length=50, blank=True, null=True)
    contest = models.CharField(max_length=500, blank=True, null=True, default='[]')

class Teacher(models.Model):
    really_a_teacher = models.BooleanField('真的是老师吗 ?', default=False)
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    username = models.CharField(max_length=50, blank=True, null=True)
    email = models.EmailField(max_length=254, blank=True, null=True)
    phone_number = models.CharField(max_length=50, blank=True, null=True)
    TID = models.CharField(max_length=50, blank=True, null=True, unique=True,)
    gender = models.CharField(max_length=50, blank=True, null=True)
    department = models.CharField(max_length=50, blank=True, null=True)
    authorization_for_contest = models.CharField(max_length=500, blank=True, null=True, default='[]')

我分别将“unique=True”添加到SID和TID,但在迁移时只有TID部分出错。

您可以创建数据迁移并在模型迁移之前运行,在该迁移中,获取具有相同TID的所有教师并为其分配唯一TID,或者如果对象都重复,则删除其中一个实例

您可以使用
python manage.py shell打开django的shell
&执行以下代码

from django.db.models import Count
Teacher.objects.values('TID').annotate(tid_count=Count('TID'))
这将以这种形式输出

[
   {'TID': '56', 'tid_count': 2}, 
   {'TID': '98', 'tid_count': 4}
]
这将帮助您调试并找出哪个TID在迁移中导致错误

关于更多细节,你们可以通过这个问题