3

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?

11
  • 1
    Did you maybe modify your migration file manually? I'd suspect that Django thinks that 0001_initial is already migrated and this is why it doesn't add this field. I'd also suggest to try python manage.py migrate appname zero to rollback all migrations and then run python manage.py migrate again. Commented Jun 28, 2017 at 18:05
  • Have you set AUTH_USER_MODEL to your custom user model? Are you seeing the [more_fields_here] fields in the database? Commented Jun 28, 2017 at 20:06
  • @Chris, I can confirm that I have set the AUTH_USER_MODEL. All other fields are in the database as expected. The problem is only with this field. Commented Jun 28, 2017 at 22:36
  • Does manage.py sqlmigrate <app_name> 0001 include SQL to create the column? Commented Jun 28, 2017 at 22:38
  • 1
    @Kamil, you can add your comment as an answer so that other people who might have a similar issue can find it more carefully. Commented Jun 29, 2017 at 15:18

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.