title: "django.db.utils.ProgrammingError: column Staff.id does not exist LINE 1: SELECT "Staff"."id", "Staff"."user_id", "Staff"."department"...."
body: <p>I have created a custom User model and have two models "Staff" and "Patient" having OneToOne relationship with my custom User model. I have successfully migrate but the error occurs when executing the second line of code </p>
  
  <pre><code>doc = User.objects.create_user(email='jlennon@beatles.com',password='glassdoor',user_type=1)
  s_doc = Staff.objects.get_or_create(user=doc)
  </code></pre>
  
  <p>, I get this ERROR:
  "django.db.utils.ProgrammingError: column Staff.id does not exist
  LINE 1: SELECT "Staff"."id", "Staff"."user_id", "Staff"."department"..."</p>
  
  <p>models.py</p>
  
  <pre class="lang-py prettyprint-override"><code>class User(AbstractUser):
  
      #use email as authentication
      username = None
      email = models.EmailField(_('email address'), unique=True)
  
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = []
  
      objects = CustomUserManager()
  
      USER_TYPE_CHOICES = (
        (1, 'doctor'),
        (2, 'labPeople'),
        (3, 'receptionist'),
        (4, 'patient'),
        (5, 'admin'),
      )
      user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES,null=True)
  
      def __str__(self):
          return self.email
    # class Meta:
    #       db_table = 'auth_user'
  
  class Staff(models.Model):
      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,related_name='staff')
      id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
      fname = models.CharField(max_length=255, blank=True, null=True)
      lname = models.CharField(max_length=255, blank=True, null=True)
      sex = models.BooleanField(blank=True, null=True)
      tel_no = models.CharField(max_length=255, blank=True, null=True)
      email = models.EmailField(max_length=255,unique=True)
      department = models.ForeignKey('Department', on_delete=models.CASCADE, db_column='department', blank=True, null=True)
      hospital = models.ForeignKey('Hospital', on_delete=models.CASCADE, db_column='hospital', blank=True, null=True)
  
      class Meta:
          # managed = False
          db_table = 'Staff'
  
  class Patient(models.Model):
      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,related_name='patient')
      email = models.EmailField(max_length=255,unique=True)
      fname = models.CharField(max_length=255, blank=True, null=True)
      lname = models.CharField(max_length=255, blank=True, null=True)
      dob = models.DateField(db_column='DOB', blank=True, null=True)  # Field name made lowercase.
      sex = models.BooleanField(blank=True, null=True)
      tel_no = models.CharField(max_length=255,blank=True,null=True)
      address = models.CharField(max_length=255, blank=True, null=True)
      nin = models.AutoField(db_column='NIN', primary_key=True)  # Field name made lowercase.
  
      class Meta:
          # managed = False
          db_table = 'Patient'
  </code></pre>
  
  <p>managers.py</p>
  
  <pre class="lang-py prettyprint-override"><code>from django.contrib.auth.base_user import BaseUserManager
  from django.utils.translation import ugettext_lazy as _
  from django.db import models
  import django
  
  
  
  class CustomUserManager(BaseUserManager):
      """
      Custom user model manager where email is the unique identifiers
      for authentication instead of usernames.
      """
      use_in_migrations = True
      def _create_user(self, email, user_type, password, **extra_fields):
          """
          Create and save a User with the given email and password.
          """
          django.setup()
          if not email:
              raise ValueError('The Email must be set')
          email = self.normalize_email(email)
          user = self.model(email=email, user_type=user_type, **extra_fields)
          user.set_password(password)
          user.save(using=self._db)
          return user
  
      def create_user(self, email, user_type, password=None, **extra_fields):
          """Create and save a regular User with the given email and password."""
          extra_fields.setdefault('is_staff', False)
          extra_fields.setdefault('is_superuser', False)
          if user_type in set([1,2,3,5]):
              is_staff = True
          return self._create_user(email, user_type, password, **extra_fields)
  
      def create_superuser(self, email, user_type, password, **extra_fields):
          """
          Create and save a SuperUser with the given email and password.
          """
          extra_fields.setdefault('is_staff', True)
          extra_fields.setdefault('is_superuser', True)
          extra_fields.setdefault('is_active', True)
  
          if extra_fields.get('is_staff') is not True:
              raise ValueError('Superuser must have is_staff=True.')
          if extra_fields.get('is_superuser') is not True:
              raise ValueError('Superuser must have is_superuser=True.')
          return self._create_user(email, user_type, password, **extra_fields)
  
  </code></pre>
  
  <p>The Traceback is as below:</p>
  
  <pre><code>Traceback (most recent call last):
    File "populate_health.py", line 90, in &lt;module&gt;
      populate()
    File "populate_health.py", line 26, in populate
      s_doc = Staff.objects.get_or_create(user=doc)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
      return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/query.py", line 538, in get_or_create
      return self.get(**kwargs), False
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/query.py", line 402, in get
      num = len(clone)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __len__
      self._fetch_all()
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
      cursor.execute(sql, params)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
      return super().execute(sql, params)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
      return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
      return executor(sql, params, many, context)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
      return self.cursor.execute(sql, params)
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File "//anaconda/envs/myhealth/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
      return self.cursor.execute(sql, params)
  django.db.utils.ProgrammingError: column Staff.id does not exist
  LINE 1: SELECT "Staff"."id", "Staff"."user_id", "Staff"."department"...
  </code></pre>
  
  <p>I first got this error when I did not define id for Staff since django just does it automatically. But I just added it to try and the result is the same. I also tried with creating a Patient object, and it would result in the same error.</p>
  
  <p>I have been stuck on this for more than one week and only found some other similar problems happen while migrating which is not the same. How is this happening?</p>