0

Found a 'silly' temporary fix for this issue -- MANUALLY add a COLUMN in PostgreSQL TABLE. Then, everything tests to be working. :) Still trying to root cause and find a permanent fix. The root cause is that RDS instance (PostgreSQL) on EC2 does not pick up the newly added field in django models. Any help is much appreciated.

Essentially, this is the same issue as python manage.py migrate does not make any changes in the postgres database

But I tried the answers in that question. It does not help.

====================================================================

The Django app has been working on AWS Elastic Beanstalk. However, after adding one field in the custom User model, it blows up!

Environment:

psql (PostgreSQL) 9.4.4
django 1.8

'django-admin makemigrations' works on both the local dev machine and AWS server. 'django-admin migrate' is VERY different.

On the local dev machine, 'django-admin migrate' works just fine.

Running migrations:
  Rendering model states... DONE
  Applying accountauth.0002_auto_20151210_2050... OK

On the AWS EC2 instance, 'django-admin migrate' complains:

Running migrations:
  Rendering model states... DONE
  Applying accountauth.0002_auto_20151210_2049...Traceback (most recent call last):
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "summary" of relation "auth_user" does not exist

After googling several hours, I found this solution

django-admin migrate --fake

It applies previously unapplied migrations:

Running migrations:
  Rendering model states... DONE
  Applying accountauth.0002_auto_20151210_2049... FAKED
  Applying accountauth.0003_auto_20151210_2056... FAKED

So, the database migrations succeed? Now, when I tried to login to the django website, the HTTP 500 ERROR happens

Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)

Have you had similar issue before? How do you solve it??

UPDATE: The model is

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

class AccountUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), blank=False, max_length=255, unique=True)
    first_name = models.CharField(_('first name'), max_length=40, blank=True, null=True, unique=False)
    last_name = models.CharField(_('last name'), max_length=40, blank=True, null=True, unique=False)

    **# Below is the newly added field**
    summary = models.TextField(_('summary'), max_length=300, blank=False)

    objects = AccountUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')
        db_table = 'auth_user'
        abstract = False

class AccountUserManager(BaseUserManager):
    def create_user(self, email, first_name=None, last_name=None, password=None):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            first_name=first_name or '',
            last_name=last_name or ''
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

UPDATE II:

I inspect the object through './manage.py shell'

>>> AccountUser.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 138, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 162, in __iter__
    self._fetch_all()
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 238, in iterator
    results = compiler.execute_sql()
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql
    cursor.execute(sql, params)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column auth_user.summary does not exist

Obviously, the summary field does not exist in PostgreSQL.

Here is how I run the migrations on server with migrate.sh

#!/bin/sh
echo ">> Deleting compiled pyc files"
find . -name '*.pyc' -exec rm {} \;

echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete

echo ">> Running manage.py makemigrations"
python manage.py makemigrations

echo ">> Running manage.py migrate"
python manage.py migrate

echo ">> Done"

One suspicious thing is that ./manage migrate always return "No migrations to apply." This is why the newly added field 'summary' never gets added in the PostgreSQL database?

Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
Community
  • 1
  • 1
thinkdeep
  • 945
  • 1
  • 14
  • 32
  • 1
    NOTE: I am directly issuing the data migration command on AWS EC2 server, as the .ebextensions container_command always fails during 'eb deploy'. Because of permission denied issue when using ec2-user to migrate, I am using the root user to issue above commands. – thinkdeep Dec 11 '15 at 05:23
  • Can you post you model and point the changes that you made before migrating? – ofnowhere Dec 11 '15 at 06:39
  • Please see update. Thank you for the help!! – thinkdeep Dec 11 '15 at 16:15
  • Fake does not apply migrations, it just makes an entry in the django_migrations table without applying those to the DB – ofnowhere Dec 11 '15 at 16:36
  • Then, the issue probably results from newly added field in django_migrations cannot be applied to PostgreSQL on AWS EC2 server. Usually, how is the newly added field in model applied to PostgreSQL on EC2 server? As this process works fine on local dev machine with PostgreSQL. – thinkdeep Dec 11 '15 at 16:45
  • Can you try migrating using "python manage.py migrate", you should first reset fake, please refer http://stackoverflow.com/questions/30626886/how-to-redo-a-migration-on-django-1-8-after-using-fake – ofnowhere Dec 11 '15 at 17:06
  • Thanks for the post. I already updated the app version on EC2 server and go through the migrations again. The same issue still exist. Please refer to another update. – thinkdeep Dec 11 '15 at 17:11

0 Answers0