1

I'm really stuck here and i'm a little confused as to why this error is being thrown. I'm running Django 1.10 and the live database is a postgresql DB. I'm not that familiar with postgres. I've been doing all of my development work with sqlite for flexibility.

I've recently added two new fields to my model. They are both IntegerFields I've not had any issues with them locally. I've not deployed my changes to my live environment and i'm getting the above error.

goal = models.IntegerField(default=1, choices=weight_goal, null=True, blank=True)

I have tried removing the fields, deleting all the migration files and even removing the model itself. However, when i add the field back in it throws the same error.

I've done some research and all i can see is 'Drop the DB and create it again". Well this isn't possible as i'll lose all of the data inside the live DB.

I don't really feel comfortable diving into a live database and trying to add the column manually. I mean, this seems a little OTT anyway?

Note: I'm deploying the box via Heroku

Profile Model:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    image = CloudinaryField('image', default="thumbnail_mqe6ne")
    weight = models.DecimalField(help_text="KG", max_digits=5, default=0, decimal_places=2, validators=[
        MinValueValidator(30),
        MaxValueValidator(600)
        ])
    height = models.DecimalField(help_text="CM", max_digits=8, default=0, decimal_places=2, validators=[
        MinValueValidator(20),
        MaxValueValidator(600),
        ])

    gender_option = (
        ('Male', 'Male'),
        ('Female', 'Female'),
    )

    profile_status = (
        ('Public', 'Public'),
        ('Private', 'Private'),
    )



    birth_date = models.DateField(blank=True, verbose_name="Date of Birth")


    gender = models.CharField(choices=gender_option, max_length=10)
    bio = models.TextField(max_length=300, blank=True)
    location = models.CharField(max_length=30, blank=True)


    weight_goal = (
        (1, 'Weight Loss'),
        (2, 'Weight / Muscle weight gain'),
    )

    goal = models.IntegerField(default=1, choices=weight_goal, null=True, blank=True)

    activity = (
        (1, 'Mostly inactive or sedentary'),
        (2, 'Fairly active'),
        (3, 'Moderately active'),
        (4, 'Active'),
        (5, 'Very active'),
    )


    activity_level = models.IntegerField(default=1, choices=activity, null=True, blank=True)


    intensity = (
        (1, 'Very light training'),
        (2, 'Moderate intensity'),
        (3, 'Moderate to high intensity (1 - 3 hours)'),
        (4, 'Very high intensity training (4+ Hours Daily)'),

    )


    training_intensity = models.IntegerField(default=1, choices=intensity)


    status = models.CharField(default="Public", max_length=10, choices=profile_status)

Any help would be much apprecited. Full traceback below:

Environment:


Request Method: GET
Request URL: /

Django Version: 1.10
Python Version: 2.7.13
Installed Applications:
['djangocms_admin_style',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 'stats',
 'home',
 'blog',
 'haystack',
 'ckeditor_uploader',
 'ckeditor',
 'django_social_share',
 'post_office',
 'sorl.thumbnail',
 'storages',
 'cloudinary',
 'django.contrib.staticfiles',
 'cloudinary_storage',
 'django_cleanup',
 'django_instagram',
 'embed_video',
 'easy_thumbnails',
 'filer',
 'reversion',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'solid_i18n.middleware.SolidLocaleMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.locale.LocaleMiddleware']



Traceback:

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/app/home/views.py" in get_user_profile
  42.     profile = Profile.objects.get(user=request.user.id)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in get
  379.         num = len(clone)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in __len__
  238.         self._fetch_all()

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1085.             self._result_cache = list(self.iterator())

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql()

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /profile/
Exception Value: column home_profile.goal does not exist
LINE 1: ... "home_profile"."bio", "home_profile"."location", "home_prof...
                                                         ^
JDavies
  • 2,730
  • 7
  • 34
  • 54
  • make sure you run migrations on the live database if you have added any field locally – Exprator Jun 06 '17 at 10:54
  • Hey! Yeah i've tried running the migrations on the heroku box also – JDavies Jun 06 '17 at 10:56
  • can you post you home_profile model? or else check if goal field exist in the model or not and also in the live server, – Exprator Jun 06 '17 at 10:57
  • I've added the Profile model. I've checked the live site and the models file is exactly the same. – JDavies Jun 06 '17 at 11:21
  • goal = models.IntegerField(default=1, choices=weight_goal, null=True, blank=True) make this as CharField then upload in server and run makemigrations first then migrate and let me know \ – Exprator Jun 06 '17 at 11:23
  • Done and it's not made any difference. In fact on my local (sqlite) it's saying - Exception Value: no such table: home_profile – JDavies Jun 06 '17 at 11:40
  • that means you table is deleted somehow? did you run any delete query? – Exprator Jun 06 '17 at 11:41
  • No, just a simple migration. It's still got the error above on the live site though. I've checked to see if the fields have updated and they have. – JDavies Jun 06 '17 at 11:42

1 Answers1

1

The first thing to do, if you haven't done already is to create a backup of your database.

database is a postgresql DB. I'm not that familiar with postgres. I've been doing all of my development work with sqlite for flexibility.

For non trivial projects, it's best to have the same RDBMS at both ends.

I've done some research and all i can see is 'Drop the DB and create it again"

You are right not to go down this path. That's very poor advice.

I have tried removing the fields, deleting all the migration files and even removing the model itself. However, when i add the field back in it throws the same error.

But this is also a pretty lonely and dangerous path that you have taken full of pitfalls. Let's see if we can put things right.

The fact that postgresql complaining about the column being missing clearly indicates that you have a rogue, migration. Go through your backups and see if you have a version of the db, with that column. If you don't, you are going to lose some data.

If you have such a backup, restore it after visually checking through psql or pgadmin that the column is indeed missing on the db.

Now that you have cleared up the migrations, completely empty that folder. Make sure that there aren't any .pyc files hanging about.

Next inspect your django_migrations table. Delete the the migrations related to the current app (home I think).

Next do

./manage.py makemigrations home

This should create exactly one migration in your migrations folder. After confirmation do

./manage.py migrate --fake-initial home

now everything should be ok.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Thanks for the detailed response :) I've been playing and backed up the DB etc. I've got to the point where i've removed the migrations files and when i run 'makemigrations' command it runs as normal and created the initial file. However, when i run 'migrate --fake-initial home' It just throws back the following: Operations to perform: Apply all migrations: home Running migrations: No migrations to apply. – JDavies Jun 06 '17 at 13:19
  • Probably because you missed deleting the relevent entries in the `django_migrations` table. It doesn't really matter at this point, but it will come back to bite oyu the next time you need to do a migration. – e4c5 Jun 06 '17 at 13:21
  • Ok, i've removed the entries for the home app inside the django_migrations. Then run the commands as above and i'm getting the same error. – JDavies Jun 06 '17 at 13:53
  • Are you sure that you have followed all the step precisely? – e4c5 Jun 06 '17 at 13:55
  • Yes. The only thing i will add is that the column didn't exist, if was a new field added to that model. So I did do a backup and it gave the me the same error on a different field. So i restored the latest backup (Back to where i was) and i'm getting the same error. I've cleared all the migrations from the folder inside the 'home' app. Then removed all of the traces of 'home' inside django_migrations. On the upside it's now giving me this - Running migrations: Rendering model states... DONE Applying home.0001_initial... FAKED – JDavies Jun 06 '17 at 14:00
  • Sorry, miunderstanding on my part. If the version of the database you have is one with the `location` field the --fake-initial shouln't be used. But your question said column didn't exists. – e4c5 Jun 06 '17 at 14:02
  • Ok, i've checked the apps table and the column 'goal' Isn't being displayed. The migration isn't adding it. So should i remove 'home' from 'django_migrations' and migrate without the fake? - Thanks for your time btw. – JDavies Jun 06 '17 at 14:05
  • yes, that's it. But your data that used to be in the location column would be lost – e4c5 Jun 06 '17 at 14:07
  • That's fine as the app isn't officially live yet, so the dataset is small. Ok, so when i run migrate home, i get the following: django.db.utils.ProgrammingError: relation "home_profile" already exists – JDavies Jun 06 '17 at 14:10
  • Because again you haven't cleared up the `django_migrations` your fake would get it populated. – e4c5 Jun 06 '17 at 14:12
  • Yeah it's still not working for some reason. I managed to get it working on my sqlite DB. However, when i cleared the migrations and migrated, it doesn't seem to add the new column / field above. I've now added it manually to the DB and it's working. Not the way i wanted to do it, but i have a backup of the DB so if i can workout why, then i'll revert back and resolve. – JDavies Jun 07 '17 at 12:44
  • 1
    I thnk there is some additional iformation that hasn't been revealed. Otherwise thsi recipe works (and as you say you have found it to work on sqlite already). Anyhow, if you have a working installation, simply mark the migration has been applied using fake and all will be well. – e4c5 Jun 08 '17 at 10:42
  • Yeah sure. Thank you for your patience and help. Much appreciated. – JDavies Jun 08 '17 at 12:57
  • No worries. Glad to have been of help. I am glad to know you think I was patient. Reading some of my comments, they don't sound like that. All the best with your project. – e4c5 Jun 08 '17 at 12:58