Django Migration Error: Column does not exist

后端 未结 15 2226
北恋
北恋 2020-12-10 10:25

Python 3, Django 1.8.5, Postgres

I have a model Sites that has been working fine. I recently tried to add a field, airport_code, and migrate the data.

相关标签:
15条回答
  • 2020-12-10 10:48

    I faced this problem. for solving problem follow the step.
    1.open Database and table.
    2.Create a required(sites_site.airport_codes in question ) column which was not exits in your table with default value.

    3.run the command

    python manage.py makemigrations

    python manage.py migrate <app name>

    python manage.py runserver

    0 讨论(0)
  • 2020-12-10 10:49

    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.

    0 讨论(0)
  • 2020-12-10 10:49

    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.

    0 讨论(0)
  • 2020-12-10 10:49

    I ran into this problem recently after upgrading to Django 1.11. I wanted to permanently address the issue so I wouldn't have to comment / uncomment code every time I ran a migration on the table, so my approach:

    from django.db.utils import ProgrammingError as AvoidDataMigrationError
    
    try:
        ... do stuff that breaks migrations
    except AvoidDataMigrationError:
        pass
    

    I rename the exception during import to AvoidDataMigrationError so it's clear why it's there.

    0 讨论(0)
  • 2020-12-10 10:50

    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.

    0 讨论(0)
  • 2020-12-10 10:51

    I too got same issue when i executed a cmd like python manage.py makemigrations app1. I resolved my issue by commenting the custom orms which is running in another app2 under views.

    Example:

    inside app1 models.py

    class ModelA(models.Model):
        subject = models.CharField(max_length=1)
        course = models.CharField(max_length=1)
    

    inside app2 view.py

    # obj = ModelA.object.all()
    # get_subjct = [s.subject for s in obj]
    

    So here i have commented above code and executed the makemigrations and migrate then uncommented.

    Working fine...

    0 讨论(0)
提交回复
热议问题