Django Migration Error: Column does not exist

后端 未结 15 2294
北恋
北恋 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 11:11

    Run into this problem after the migration of my postgres database to a differnt server. Somehow I messed up the database and could not update my model with the new class UserProfile.

    I solved the problem creating initial migration for existing schema:

    1. Empty the django_migrations table: delete from django_migrations;with a command DELETE FROM django_migrations WHERE app='my_app';
    2. For every app, delete its migrations folder: rm -rf /migrations/
    3. Reset the migrations for the "built-in" apps: python manage.py migrate --fake
    4. For each app run: python manage.py makemigrations . Take care of dependencies (models with ForeignKey's should run after their parent model).
    5. Finally: python manage.py migrate --fake-initial

    Got it here: https://stackoverflow.com/a/29898483

    PS I'm not sure that this was relevant to the resolution of the problem but, first, I dropped the table in postgresql that caused an error and commented out the UserProfile class in models.

    in shell:

     sudo -su postgres
     psql databse_name   
     DROP TABLE table_name;
    

    models.py:

    #class UserProfile(models.Model):
        #user = models.OneToOneField(settings.AUTH_USER_MODEL, unique=True, primary_key=True, on_delete=models.CASCADE, related_name='user_profile')
        #avatar = ThumbnailerImageField(upload_to='profile_images', blank=True)
        #country = models.CharField(max_length = 128)
    

提交回复
热议问题