Skip to content
Advertisement

Django test error: Column does not exist

Postgresql, Django 2.0, Python 3.6.4

After running a migration that changed the name of a field desktop_pay to simply pay, I’m getting an error when running manage.py test saying the column pay does not exist.

Here’s the migration:

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('instructions', '0055_auto_20180517_1508'),
    ]

    operations = [
        migrations.RenameField(
            model_name='instruction',
            old_name='desktop_pay',
            new_name='pay',
        ),
    ]

Here’s the error:

> python .manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "C:Python36libsite-packagesdjangodbbackendsutils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column instructions_instruction.pay does not exist
LINE 1: "...on"."quota", "instructions_instruction"."target", "instructi...
                                                              ^

If I start a psql prompt, though, I can clearly see that the column does exist, at least in the “real” table.

mydatabase=> d+ instructions_instruction
                                                              Table "public.instructions_instruction"
       Column        |          Type          | Collation | Nullable |                       Default                        | Storage  | Stats target | Description
---------------------+------------------------+-----------+----------+------------------------------------------------------+----------+--------------+-------------
 id                  | integer                |           | not null | nextval('instructions_instruction_id_seq'::regclass) | plain    |              |
 quota               | smallint               |           | not null |                                                      | plain    |              |
 pay                 | numeric(5,2)           |           | not null |                                                      | main     |              |

### etc...

What’s going on here? Why is Django not finding the column? How can I debug this?

Advertisement

Answer

I’ve run into these ‘column does not exist’ errors when my migrations somehow got messed up, sometimes this has happened when I accidentally deleted/overwrote migrations, but I’ve also had it happen just by running typical migrations which I can’t really explain.

So I would guess you would have to pinpoint the problem in your migrations. What worked for me was, after confirming my schema was in fact as I wanted to be, resetting migrations with the help of Scenario #2 here.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement