Django Migration is not applying the migration changes

后端 未结 8 689
悲哀的现实
悲哀的现实 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.

    0 讨论(0)
  • 2021-02-02 16:02

    Most of the above solutions would help in the issue, however, I wanted to point out another possible (albeit rare) possibility that the allow_migrate method of database router may be returning False when it should have returned None.

    Django has a setting DATABASE_ROUTERS which will be used to determine which database to use when performing a database query.

    From the docs:

    if you want to implement more interesting database allocation behaviors, you can define and install your own database routers.

    A database router class implements up to four methods:

    • db_for_read(model, **hints)
    • db_for_write(model, **hints)
    • allow_relation(obj1, obj2, **hints)
    • allow_migrate(db, app_label, model_name=None, **hints)

    From the documentation:

    allow_migrate(db, app_label, model_name=None, **hints)

    Determine if the migration operation is allowed to run on the database with alias db. Return True if the operation should run, False if it shouldn’t run, or None if the router has no opinion.

    It is possible that one of the database routers in sequence is returning False for the migration that you're trying to run, in which case the particular operation will not be run.

    0 讨论(0)
  • 2021-02-02 16:03

    In addition to the other answers, make sure that in models.py, you have managed = True in each table's meta

    0 讨论(0)
  • 2021-02-02 16:05

    In my case, the migrations were not being reflected in mysql database. I manually removed the row of 'myapp'(in your case 'myproj') from the table 'django_migrations' in mysql database and ran the same commands again for migration.

    0 讨论(0)
  • 2021-02-02 16:07

    Make sure that the migrations/ folder contains a __init__.py file

    Lost half an hour over that.

    0 讨论(0)
  • 2021-02-02 16:12
    pip install django-extensions
    

    and add this in the install app of settings.py

    INSTALLED_APPS = [
        'django_extensions'
    ]
    

    Then run

    python ./manage.py reset_db
    

    Then run migrations again

    python manage.py makemigrations
    python manage.py migrate
    
    

    Now, run migrations for your installed apps

    python manage.py makemigrations your_app_name
    python manage.py migrtate your_app_name
    
    

    Done! See Your Database...

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