Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django does not create ImageField on Postgres

I added a new ImageField in my 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 without any errors.

But when I run my application I am getting:

ProgrammingError at column authentication_user.profile_picture does not exist

I checked in the Postgres database and the column profile_picture does not exist.

I deleted the migrations and tried again, but I am still getting the same error.

In the migrations/0001_initial.py there is the line:

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

But why does the column not exist in the table?

like image 700
Galil Avatar asked Apr 26 '26 13:04

Galil


1 Answers

It looks like it was something messed with migrations, it's not recommended to modify migration files manually. Django stores information which migrations were already applied, if you modify 0001 migration which is already applied and run migrate again those modifications won't be applied. Of course I don't know if this exactly what happened to you, but it looks like profile_picture field was added after 0001 was applied.

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

  1. remove field profile_picture from 0001 migration
  2. run makemigrations again (0002 with new field profile_picture should be created)
  3. run migrate
like image 85
Kamil Avatar answered Apr 28 '26 08:04

Kamil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!