表的多个主键" app_employee"不允许。

时间:2017-04-06 02:54:33

标签: django postgresql

使用PostgreSQL的Django 1.11。

我要迁移我的网站,models.py会抛出我无法拥有多个主键的错误。我无法看到我的位置(或者我不理解如何)。

class Employee(models.Model):
    Aegis_ID = models.UUIDField(primary_key=True, null=False, default=uuid.uuid4, editable=False, serialize=True)
    Employee_Number = models.ForeignKey('self', on_delete=models.CASCADE, related_name='Company_Employee_Number', 
                                    null=True, blank=True, max_length=6, help_text="Employee ID")
    Employee_FName = models.CharField(null=True, blank=True, max_length=25, help_text="First Name")
    Employee_LName = models.CharField(null=True, blank=True, max_length=25, help_text="Last Name")
    Employee_Email = models.EmailField(max_length=80, blank=True, help_text="GPM Email address")
    Employee_Position = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, 
                                      related_name='Department_Employee_Position', max_length=3, 
                                      choices=EMPLOYEE_POSITION, help_text="Select position of this Employee.")
    Hire_Date = models.DateField(null=True, blank=True, help_text="Enter the employee hire date.")
    Employee_Division = models.CharField(max_length=2, null=True, blank=True, choices=DIVISION_CHOICES,
                                     help_text="Assign the Audit Division for this employee.")
    Employee_Region = models.CharField(max_length=3, null=True, blank=True, choices=REGION_CHOICES,
                                   help_text="Assign the Audit Region for this employee.")
    Employee_District = models.CharField(max_length=3, null=True, blank=True, choices=DISTRICT_CHOICES,
                                     help_text="Assign the Audit District for this Employee.")

阅读关于这个确切主题的Django页面,它被列为1.7中解决的问题,并且与Django按字母顺序按类排序表有关。

我在python manage.py flush

之前尝试了makemigrations,然后是migrate

那么,Django / Postgres试图制作一个" id"和"主要"因为我只是不理解,这里......

根据关于自动主键的Django文档,看不见的是id = models.AutoField(primary_key=True),但我也明白如果你将primary_key=True分配给一个字段,这不适用

2 个答案:

答案 0 :(得分:2)

在您的上述模型中不允许使用表“app_employee”的多个主键。

没有因为你有

Aegis_ID = models.UUIDField(primary_key=True, null=False, default=uuid.uuid4, editable=False, serialize=True)

因为在django文档中明确指出了

  

Django文档

Field.primary_key 如果为True,则此字段是模型的主键。

如果你没有为模型中的任何字段指定primary_key = True,Django会自动添加一个AutoField来保存主键,因此你不需要在任何字段上设置primary_key = True,除非你想要覆盖默认的主键行为。

primary_key = True表示null = False且unique = True。对象上只允许使用一个主键。

我已经在我的项目上尝试了你的模型,它工作得非常好。 为简单起见,我删除了其他字段

  

models.py

from __future__ import unicode_literals
from django.db import models
import uuid

class Employee(models.Model):
    Aegis_ID = models.UUIDField(primary_key=True, null=False,default=uuid.uuid4, editable=False, serialize=True)
    Employee_Number = models.ForeignKey('self', on_delete=models.CASCADE, related_name='Company_Employee_Number', 
                                null=True, blank=True, max_length=6, help_text="Employee ID")
    Employee_FName = models.CharField(null=True, blank=True, max_length=25, help_text="First Name")
    Employee_LName = models.CharField(null=True, blank=True, max_length=25, help_text="Last Name")
    Employee_Email = models.EmailField(max_length=80, blank=True, help_text="GPM Email address")

当我做的时候

(venv) astikanand@Developer-PC:~/firstsite$ python manage.py makemigrations
Migrations for 'employee':
employee/migrations/0001_initial.py
- Create model Employee

然后

(venv) astikanand@Developer-PC:~/firstsite$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, employee, sessions
Running migrations:
Applying employee.0001_initial... OK

所以正常工作

  

你需要做的是

要么重新创建应用程序,要么只是重新启动项目,可能是一些依赖性问题或其他问题。但是你的模型Employee的代码都没问题。

答案 1 :(得分:0)

您很可能更改了主键和/或对其他模型/表的引用,而迁移文件中仍然保留了一些旧式依赖项。

请参考官方Django文档以恢复过去的迁移。您不必重新启动项目即可删除依赖关系。

python manage.py makemigrations --empty yourappname

就是这样。然后,检查应用程序中migrations文件夹下的0001_initial.py文件,以确保已删除所有依赖项。看起来应该像这样:

from django.db import migrations

class Migration(migrations.Migration):

dependencies = [
    ('yourappname', '0001_initial'),
]

operations = [
]
相关问题