2

I am extending User on django and didn't realize I need to add phone number. When I made the model below, I forgot to makemigrations migrate before creating an AppUser. So I had made an AppUser/User combo as normal, but before the db was ready. It asks me to provide a default and I provided the string '1' because it wouldn't take 1 as integer

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import RegexValidator


class AppUser(models.Model):
    user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], max_length=16, blank=True) # validators should be a list

    def __str__(self):
        return self.first_name + " " + self.last_name + ": " + self.email

Now I can't touch User or AppUser:

In [4]: appusers = AppUser.objects.all()

In [5]: for user in appusers:
   ...:     user.delete()
   ...:     
---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/backends/utils.py in execute(self, sql, params)
     63             else:
---> 64                 return self.cursor.execute(sql, params)
     65 

ProgrammingError: column users_appuser.id does not exist
LINE 1: SELECT "users_appuser"."id", "users_appuser"."user_id", "use...
               ^


The above exception was the direct cause of the following exception:

ProgrammingError                          Traceback (most recent call last)
<ipython-input-5-fd4a00b110e0> in <module>()
----> 1 for user in appusers:
      2     user.delete()
      3 

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/models/query.py in __iter__(self)
    254                - Responsible for turning the rows into model objects.
    255         """
--> 256         self._fetch_all()
    257         return iter(self._result_cache)
    258 

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/models/query.py in _fetch_all(self)
   1085     def _fetch_all(self):
   1086         if self._result_cache is None:
-> 1087             self._result_cache = list(self.iterator())
   1088         if self._prefetch_related_lookups and not self._prefetch_done:
   1089             self._prefetch_related_objects()

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/models/query.py in __iter__(self)
     52         # Execute the query. This will also fill compiler.select, klass_info,
     53         # and annotations.
---> 54         results = compiler.execute_sql()
     55         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
     56                                                   compiler.annotation_col_map)

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/models/sql/compiler.py in execute_sql(self, result_type)
    833         cursor = self.connection.cursor()
    834         try:
--> 835             cursor.execute(sql, params)
    836         except Exception:
    837             cursor.close()

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/backends/utils.py in execute(self, sql, params)
     77         start = time()
     78         try:
---> 79             return super(CursorDebugWrapper, self).execute(sql, params)
     80         finally:
     81             stop = time()

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/backends/utils.py in execute(self, sql, params)
     62                 return self.cursor.execute(sql)
     63             else:
---> 64                 return self.cursor.execute(sql, params)
     65 
     66     def executemany(self, sql, param_list):

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/utils.py in __exit__(self, exc_type, exc_value, traceback)
     92                 if dj_exc_type not in (DataError, IntegrityError):
     93                     self.wrapper.errors_occurred = True
---> 94                 six.reraise(dj_exc_type, dj_exc_value, traceback)
     95 
     96     def __call__(self, func):

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/utils/six.py in reraise(tp, value, tb)
    683             value = tp()
    684         if value.__traceback__ is not tb:
--> 685             raise value.with_traceback(tb)
    686         raise value
    687 

/home/cchilders/.virtualenvs/austin_eats/lib/python3.5/site-packages/django/db/backends/utils.py in execute(self, sql, params)
     62                 return self.cursor.execute(sql)
     63             else:
---> 64                 return self.cursor.execute(sql, params)
     65 
     66     def executemany(self, sql, param_list):

ProgrammingError: column users_appuser.id does not exist
LINE 1: SELECT "users_appuser"."id", "users_appuser"."user_id", "use...

Same for other:

In [3]: for user in users:
   ...:     user.delete()

blah blah

ProgrammingError: column users_appuser.user_id does not exist
LINE 1: DELETE FROM "users_appuser" WHERE "users_appuser"."user_id" ...

Can I repair this filth without dropping and remaking entire db? Thank you

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • 1
    Have you checked "AUTH_USER_MODEL" variable in settings.py file?? – Rajesh Oct 10 '16 at 04:02
  • 1
    there is no setting like that. this happened after trying to make AppUser before running migrations. now I run migrations and try to use postgres – codyc4321 Oct 10 '16 at 04:35

1 Answers1

0

This is happening because you are migrating both your apps and admin models at the same time.

To fix this,

  1. Delete Database,
  2. Delete migrations,
  3. Migrate admin models first before applying makemigrations
  4. Do make migrations and migrate.