Django Migration Error: Column does not exist
This bug was resolved for me by commenting out the django debug toolbar from INSTALLED_APPS in settings.py. I am not sure why debug toolbar is the culprit, but after I commented it out, I was able to run makemigrations
and migrate
with no issue.
Hoping this helps someone, as I spent twelve hours trying to figure it out.
In my case, that was because I had a unique_together constraint that was set.
When I wanted to remove a field, the auto-generated migration tried to remove the field before removing the unique_together constraint.
What I had to do was manually move up the migrations.AlterUniqueTogether constraint in the migration file, so that django removes first the constraint before trying to remove the field.
I hope this can help someone.
After you run makemigrations, be sure to go through the stack trace step by step.
In my case, I noticed it traced through a call to a Form contained within a forms.py in a completely different app, which happened to have a call to the model I was trying to create a new migration for.
Moving the Form class out of forms.py into the views.py fixed the issue.
I ran into this issue as well and the answer by @Nexus helped. I thought I'd provide details of my specific case here for better illustrate the cause of the issue. It seems like a potential bug to me.
I have a model Brand
as follows:
class Brand(BaseModelClass):
name = CharField(max_length=256, unique=True)
website = ForeignKey(URL, on_delete=CASCADE, null=True, blank=True)
I was running a python manage.py makemigrations
after adding a Boolean
field as follows:
class Brand(BaseModelClass):
name = CharField(max_length=256, unique=True)
website = ForeignKey(URL, on_delete=CASCADE, null=True, blank=True)
trusted = Boolean(default=True)
When running the makemigrations
command, I recieved an error similar to OP's:
django.db.utils.ProgrammingError: column appname_brand.trusted does not exist
Per @Nexus' suggestion, I went through the stacktrace, line-by-line, assuming that it wasn't some core issue with Django. As it turns out, in one of apps forms.py
file I had the following:
choices={(str(brand.id), brand.name) for brand in Brand.objects.all()}
The solution was to simply comment-out that line, run manage.py makemigrations
, then run manage.py migrate
. Afterwards, I uncommented the line and everything and my forms' functionality worked just as before.