Django迁移错误

时间:2019-01-09 11:00:13

标签: django django-models

我试图迁移这些模型并出现错误。我添加了最初不在我的模型中的字段位置管理器。使project_name在项目模型中成为主键:我跑了python manage.py migrate,但遇到错误: 下面是模型。

class Employee(models.Model):
    position = models.CharField(max_length=200)
    manager= models.CharField(max_length=200,default="x")
    employee_number = models.PositiveIntegerField(primary_key=True)


class Project(models.Model):
    project_code=models.CharField(max_length=200)   
    employees = models.ManyToManyField(Employee,through="User_Projects")
    project_name= models.TextField(primary_key=True)

class User_Projects(models.Model):
    employee_number = models.ForeignKey(Employee,to_field='employee_number',on_delete=models.CASCADE)
    project_name = models.ForeignKey(Project,to_field="project_name",on_delete=models.CASCADE)

我遇到的错误如下,我需要帮助:

InternalError:(1829,"cannot drop column 'id' : needed in a foreign key constraint 'timeapp_user_projects_projects_id_cf8c73ba_fk_timeapp_project_id' of table 'timesheets.timeapp_user_projects'")

1 个答案:

答案 0 :(得分:0)

检查您的迁移文件,django可能已经生成了删除字段'id',然后创建了alter命令。您可以编辑迁移文件以删除“ RemoveField”,然后尝试迁移。 python manage.py migration