Django does not create ImageField for Postgres

I added a new ImageField to mine models.py

:

class User(AbstractUser):
    [more_fields_here]
    profile_picture = models.ImageField(upload_to='profile_pictures', null=True)

      

I ran python manage.py makemigrations

and then python manage.py migrate

no error.

But when I run my application I get:

ProgrammingError at column authentication_user.profile_picture does not exist

      

I checked in Postgres database and the column profile_picture

doesn't exist.

I removed the migration and tried again, but I still get the same error.

In migrations/0001_initial.py

there is a line:

('profile_picture', models.ImageField(null=True, upload_to='profile_pictures')),

      

But why doesn't the column exist in the table?

+3


source to share


1 answer


It looks like it was something confusing with migrations, it is not recommended to manually modify the migration files. Django keeps track of which migrations have already been applied, if you change a migration 0001

that is already applied and running migrate

, those changes will not be applied. Of course, I don't know if this was exactly what happened to you, but it looks like the field profile_picture

was added after applying 0001

.

The easiest way to fix this (without rolling back any migrations):



  • remove field profile_picture

    from 0001

    migration
  • start makemigrations

    again ( 0002

    with new field profile_picture

    )
  • run migrate

+1


source







All Articles