acrane
acrane

Reputation: 113

column <column> does not exist (Django 1.8)

I am trying to interact with the development server for my django project. However any page on the server returns the same error:

Exception Type: ProgrammingError

Exception Value: column myApp_verb.date does not exist

I had not recently added the field date to the model verb (it's been there for a while, and I'm not sure what caused this error to begin). My collaborators all have identical files on their local machines, and none of them are having any issues.

I have tried various things:

I have tried removing the date field (and all references to it). makemigrations did not detect any changes, and migrate failed with error:

django.db.utils.ProgrammingError: column "date" does not exist

I have tried renaming the field. Once again makemigrations did not detect any changes, and migrate failed with the same error as above.

I have tried deleting all of my migrations. This changed nothing.

I'm out of ideas at this point. Any help would be much appreciated.

Thanks in advance!

Edit: Here is the verb class, as requested. Its pretty simple:

class Verb(models.Model):
    english_gloss = models.CharField(max_length = 20)
    first_person = models.CharField(max_length = 20)
    second_person = models.CharField(max_length = 20)
    third_person = models.CharField(max_length = 20)
    fourth_person = models.CharField(max_length = 20)
    transitivity = models.BooleanField()
    classifier = models.CharField(max_length = 20)
    inner_lexical = models.CharField(max_length = 20)
    outer_lexical = models.CharField(max_length = 20)
    perfective = models.CharField(max_length = 20)
    imperfective = models.CharField(max_length = 20)
    date = models.DateTimeField(auto_now_add = True)

Upvotes: 6

Views: 7456

Answers (7)

Pascal
Pascal

Reputation: 11

in my case i got this error when i first used models.ForeignKey() then changed to OneToOneFielf(). I just dropped the database then created a brand new one, ran migrations and it worked

Upvotes: 0

Justin
Justin

Reputation: 1

You can usually get around this type of error by "tricking" django into thinking the column exists while generating the migration.

  • create a new database column with the same name manually in your local db
  • run python manage.py makemigrations to generate the correct migration
  • delete the column you created earlier
  • run python manage.py migrate 'appname' to create the new column

Upvotes: 0

erika_dike
erika_dike

Reputation: 351

I have had this problem. I fixed it by going to the Django migrations table in the database and locating the migration that was faulty. In my case, it was a fairly recent migration. I deleted that migration and the others following it. Then retried running python manage.py makemigrations and python manage.py migrate. This time both commands ran and the error went away.

Upvotes: 0

Fully agree with Özer S. Yes, this is the only solution to overcome this problem. Dear Django developers, why when trying to add a field to an existing table, Django suddenly responds that such a field does not exist? Of course it does not exist, so I'm trying to add it!

The same solution is for my case - add a field with a multiple choice:

  1. This is what the added field in models.py looks like:

    TEMPLATE = (
      ('list', 'List'), ('table', 'Table')
    )
    template = models.CharField(
        'View template',
        choices=TEMPLATE,
        max_length=7,
        default='list'
    ) 
    
  2. Create manually a migration file with the number following the previous migration. I add the "template" field to the "blogcategory" table in the "blog" application and my migration file is called "0005_template".

  3. And this is the contents of the migration file (0005_template.py):

    # -*- coding: utf-8 -*-
    from django.db import migrations, models
    
    class Migration(migrations.Migration):
    dependencies = [
        ('blog', '0004_auto_20170628_1533'),
    ]
    
    operations = [
        migrations.AddField(
            model_name='blogcategory',
            name='template',
            field=models.CharField(
                verbose_name='View template',
                choices=[('list', 'List'), ('table', 'Table')],
                max_length=7,
                default='list'
            ),
        ),
    ]
    
  4. Next, comment on these lines in the model:

    TEMPLATE = (
      ('list', 'List'), ('table', 'Table')
    )
    template = models.CharField(
       'View template',
       choices=TEMPLATE,
       max_length=7,
       default='list'
    )
    
  5. Then, do the application migration in the database:

    python manage.py migrate blog
    

    and get

    Operations to perform:
        Apply all migrations: blog
    Running migrations:
        Applying blog.0005_template... OK
    

As a result, the "template" field with the default "list" was added to all entries in the "blogcategory" table.

P.S. Do not forget to uncomment the field in the model.

Upvotes: 1

MaxCore
MaxCore

Reputation: 2728

Had same problem. I wanted to add field "slug" to City model.

Error gone when I temporary commented line with select_related('city').

for location in Location.objects.filter().select_related('city'):
    cities[str(l.id)] = l.city.id

Stack trace pointed me on that peace of code:

File "/www/loorn/apps/users/widgets.py", line 69, in LocationSelect
    for location in Location.objects.filter().select_related('city'):

Upvotes: 1

Özer
Özer

Reputation: 2106

You can create a manual migration to fix the issue.

First comment out the coloumn(s) that are throwing the error.

Then write the manual migration. Usually something like this:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models    

class Migration(migrations.Migration):   

    dependencies = [
        ('my_field', 'last_migration_filename'), # no .py
    ]        

    operations = [
        migrations.AddField(
            model_name='my_model',
            name='my_field',
            field=models.MyField(blank=True, null=True),
        ),
    ]

Then run python manage.py migrate. It will create that/those field(s).

Afterwards, uncomment the fields that cause the errors.

Worked for me like a charm.

Upvotes: 4

acrane
acrane

Reputation: 113

I still have no idea why this error began, but it appears that there was some sort of corruption in my database. I deactivated the db and started a new one, and everything works perfectly again.

Upvotes: 2

Related Questions