Django 1.7 - makemigrations not detecting changes

前端 未结 29 1319
忘了有多久
忘了有多久 2020-11-27 12:43

As the title says, I can\'t seem to get migrations working.

The app was originally under 1.6, so I understand that migrations won\'t be there initially, and indeed i

相关标签:
29条回答
  • 2020-11-27 13:04

    Agree with @furins. If everything seems to be in order and yet this problem arises, checkout if there is any property method with same title as the attribute which you are trying to add in the Model class.

    1. Remove method with similar name as attribute you are adding.
    2. manage.py makemigrations my_app
    3. manage.py migrate my_app
    4. Add the methods back.
    0 讨论(0)
  • 2020-11-27 13:07

    Make sure your model is not abstract. I actually made that mistake and it took a while, so I thought I'd post it.

    0 讨论(0)
  • 2020-11-27 13:08

    This may happen due to the following reasons:

    1. You did not add the app in INSTALLED_APPS list in settings.py (You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS
    2. You don't have migrations folder inside those apps. (Solution: just create that folder).
    3. You don't have __init__.py file inside migrations folder of those apps. (Solution: Just create an empty file with name __init__.py)
    4. You don't have an __init__.py file inside the app folder. (Solution: Just create an empty file with name __init__.py)
    5. You don't have a models.py file in the app
    6. Your Python class (supposed to be a model) in models.py doesn't inherit django.db.models.Model
    7. You have some semantic mistake in definition of models in models.py

    Note: A common mistake is to add migrations folder in .gitignore file. When cloned from remote repo, migrations folder and/or __init__.py files will be missing in local repo. This causes problem.

    I suggest to gitignore migration files by adding the following lines to .gitignore file

    */migrations/*
    !*/migrations/__init__.py
    
    0 讨论(0)
  • 2020-11-27 13:08

    Maybe this will help someone.

    I've deleted my models.py and expected makemigrations to create DeleteModel statements.

    Remember to delete *.pyc files!

    0 讨论(0)
  • 2020-11-27 13:08
    ./manage makemigrations
    ./manage migrate
    

    Migrations track changes to DB so if youre changing from unmanaged to managed, you'll need to make sure that youre database table is up to date relating to the Model you're dealing with.

    If you are still in dev mode, I personally decided to delete the migration files in my IDE as well as in the django_migrations table relating to my Model and rerun the above command.

    REMEMBER: if you have a migration that ends with _001 in your IDE & _003 in your database. Django will only see if you have a migration ending with _004 for anything to update.

    The 2 (code & db migrations) are linked and work in tandem.

    Happy coding.

    0 讨论(0)
  • 2020-11-27 13:09

    Maybe I am too late but did you try to have a migrations folder in your app with a __init__.py file in it?

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