Django Migration is not applying the migration changes

后端 未结 8 702
悲哀的现实
悲哀的现实 2021-02-02 15:41

Using django 1.7.7 I want to use django\'s migration to add or remove a field. so I modified model.py and ran

python manage.py makemigrations myproj
Migrations f         


        
8条回答
  •  遇见更好的自我
    2021-02-02 16:00

    Deleting the migration directory is never a good idea, because Django then loses track of which migration has been applied and which not (and once an app is deployed somewhere it can become quite difficult to get things back in sync).

    Disclaimer: whenever things like that occur it is best to backup the database if it contains anything valuable. If in early development it is not necessary, but once things on the backend get out of sync there is a chance of things getting worse. :-)


    To recover, you could try resetting your models to match exactly what they were before you have added/removed the fields. Then you can run

    $ python manage.py makemigrations myproj
    

    which will lead to an initial migration (0001_initial...). Then you can tell Django to fake that migration, which means to tell it to set its internal counter to this 0001_initial:

    With Django 1.7:

    $ python manage.py migrate myproj
    

    With Django >= 1.8:

    $ python manage.py migrate myproj --fake-initial
    

    Now, try to change your model and run makemigrations again. It should now create a 0002_foobar migration that you could run as expected.

提交回复
热议问题