Django - makemigrations - No changes detected

前端 未结 30 1305
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 04:44

I was trying to create migrations within an existing app using the makemigrations command but it outputs \"No changes detected\".

Usually I create new apps using the

相关标签:
30条回答
  • 2020-12-02 05:28

    There are multiple possible reasons for django not detecting what to migrate during the makemigrations command.

    1. migration folder You need a migrations package in your app.
    2. INSTALLED_APPS You need your app to be specified in the INSTALLED_APPS .dict
    3. Verbosity start by running makemigrations -v 3 for verbosity. This might shed some light on the problem.
    4. Full path In INSTALLED_APPS it is recommended to specify the full module app config path 'apply.apps.MyAppConfig'
    5. --settings you might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings
    6. specify app name explicitly put the app name in manage.py makemigrations myapp - that narrows down the migrations for the app alone and helps you isolate the problem.
    7. model meta check you have the right app_label in your model meta

    8. Debug django debug django core script. makemigrations command is pretty much straight forward. Here's how to do it in pycharm. change your script definition accordingly (ex: makemigrations --traceback myapp)

    Multiple databases:

    • Db Router when working with django db router, the router class (your custom router class) needs to implement the allow_syncdb method.

    makemigrations always creates migrations for model changes, but if allow_migrate() returns False,

    0 讨论(0)
  • 2020-12-02 05:29

    In my case i forgot to insert the class arguments

    Wrong:

    class AccountInformation():
    

    Correct

    class AccountInformation(models.Model):
    
    0 讨论(0)
  • 2020-12-02 05:30

    My problem was much simpler than the above answers and probably a far more common reason as long as your project is already set up and working. In one of my applications that had been working for a long time, migrations seemed wonky, so in a hurry, I did the following:

    rm -r */migrations/*
    rm db.sqlite3
    python3 manage.py makemigrations
    No changes detected
    

    Whaat??

    I had mistakenly also removed all the __init__.py files :( - Everything was working again after I went in and:

    touch ads1/migrations/__init__.py
    

    For each of my applications then the makemigrations worked again.

    It turns out that I had manually created a new application by copying another and forgot to put the __init__.py in the migrations folder and that confinved me that everything was wonky - leading my making it worse with an rm -r as described above.

    Hope this helps someone from swearing at the "No changes detected" error for a few hours.

    0 讨论(0)
  • 2020-12-02 05:30

    When adding new models to the django api application and running the python manage.py makemigrations the tool did not detect any new models.

    The strange thing was that the old models did got picked by makemigrations, but this was because they were referenced in the urlpatterns chain and the tool somehow detected them. So keep an eye on that behavior.

    The problem was because the directory structure corresponding to the models package had subpackages and all the __init__.py files were empty. They must explicitly import all the required classes in each subfolder and in the models __init__.py for Django to pick them up with the makemigrations tool.

    models
      ├── __init__.py          <--- empty
      ├── patient
      │   ├── __init__.py      <--- empty
      │   ├── breed.py
      │   └── ...
      ├── timeline
      │   ├── __init__.py      <-- empty
      │   ├── event.py
      │   └── ...
    
    0 讨论(0)
  • 2020-12-02 05:31

    I had a different issue while creating a new app called deals. I wanted to separate the models inside that app so I had 2 model files named deals.py and dealers.py. When running python manage.py makemigrations I got: No changes detected.

    I went ahead and inside the __init__.py which lives on the same directory where my model files lived (deals and dealer) I did

    from .deals import *
    from .dealers import *
    

    And then the makemigrations command worked.

    Turns out that if you are not importing the models anywhere OR your models file name isn't models.py then the models wont be detected.

    Another issue that happened to me is the way I wrote the app in settings.py:

    I had:

    apps.deals
    

    It should've been including the root project folder:

    cars.apps.deals
    
    0 讨论(0)
  • 2020-12-02 05:32

    To create initial migrations for an app, run makemigrations and specify the app name. The migrations folder will be created.

    ./manage.py makemigrations <myapp>
    

    Your app must be included in INSTALLED_APPS first (inside settings.py).

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