【问题标题】:Multiple primary keys for table "app_employee" are not allowed.不允许表“app_employee”的多个主键。
【发布时间】:2017-04-06 02:54:32
【问题描述】:

带有 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”和“primary”的字段是什么,因为我只是不明白,在这里...

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

【问题讨论】:

    标签: django postgresql


    【解决方案1】:

    在您的上述模型中不允许表“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 的代码一切正常。

    【讨论】:

      【解决方案2】:

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

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

      python manage.py makemigrations --empty yourappname
      

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

      from django.db import migrations
      
      class Migration(migrations.Migration):
      
      dependencies = [
          ('yourappname', '0001_initial'),
      ]
      
      operations = [
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-05
        • 2015-12-21
        • 2013-12-29
        • 1970-01-01
        • 2018-08-04
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        相关资源
        最近更新 更多