Shawn Gordon
Shawn Gordon

Reputation: 173

Multiple primary keys for table "app_employee" are not allowed.

Django 1.11 with PostgreSQL.

I go to migrate my site and models.py throws the error that I can't have more than one primary key. I can't see where I do (or I'm not understanding how).

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.")

Reading the Django pages on this exact topic, it's listed as a problem resolved in 1.7 and had to do with how Django sorted the tables by class, alphabetically.

I've also tried python manage.py flush followed by makemigrations prior to migrate

So, what fields is Django / Postgres attempting to make an "id" and "primary" because I'm just not understanding, here...

According to the Django documentation regarding Automatic Primary Keys, there's the unseen is id = models.AutoField(primary_key=True) but I also understood that if you assign the primary_key=True to a field, this did not apply

Upvotes: 2

Views: 6966

Answers (2)

Elcin Guseynov
Elcin Guseynov

Reputation: 535

It is quiet possible that you changed primary keys and/or references to other models/tables and some legacy dependencies remained in the migration files.

Please refer to the official Django documentation for reverting past migrations. You do not have to restart your project to remove dependencies.

python manage.py makemigrations --empty yourappname

That is it. Then check 0001_initial.py file under migrations folder in your app to make sure all dependencies have been removed. It should look like:

from django.db import migrations

class Migration(migrations.Migration):

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

operations = [
]

Upvotes: 0

Astik Anand
Astik Anand

Reputation: 13047

In your above model Multiple primary keys for table “app_employee” are not allowed.

It is not coming because you have

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

Because in django documentation it is clearly specified that

Django Documentation

Field.primary_key If True, this field is the primary key for the model.

If you don’t specify primary_key=True for any field in your model, Django will automatically add an AutoField to hold the primary key, so you don’t need to set primary_key=True on any of your fields unless you want to override the default primary-key behaviour.

primary_key=True implies null=False and unique=True. Only one primary key is allowed on an object.

I have tried your model on my project and it is working absolutely fine. For simplicity I removed other fields

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")

and when I did

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

and then

(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

so it is working fine.

You need to do is

Either you recreate your app or simply start your project all over again, may be some dependency issues or something. But your code for model Employee is all ok.

Upvotes: 4

Related Questions