如果你更新了models.py里的类,想更新到数据表里。
python manage.py makemigrations
提示
No changes detected
app\migrations\__init__.py文件打开是空的, 但如果把其删除,python manage.py makemigrations时就会提示No changes detected(无检查到更改)。
这种情况,在对应app\migrations目录下重建一个空的__init__.py,就可以了。
##############################################################
To create initial migrations for an app, run makemigrations and specify the app name. The migrations folder will be created. ./manage.py makemigrations Your app must be included in INSTALLED_APPS first (inside settings.py). My problem (and so solution) was yet different from those described above. I wasn't using models.py file, but created a models directory and created the my_model.py file there, where I put my model. Django couldn't find my model so it wrote that there are no migrations to apply. My solution was: in the my_app/models/__init__.py file I added this line: from .my_model import MyModel 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, I've read many answers to this question often stating to simply run makemigrations in some other ways. But to me, the problem was in the Meta subclass of models. I have an app config that says label = (in the apps.py file, beside models.py, views.py etc). If by any chance your meta class doesn't have the same label as the app label (for instance because you are splitting one too big app into multiple ones), no changes are detected (and no helpful error message whatsoever). So in my model class I have now: class ModelClassName(models.Model): class Meta: app_label = '' # <-- this label was wrong before. field_name = models.FloatField() ... Running Django 1.10 here. I had another problem not described here, which drove me nuts. class MyModel(models.Model): name = models.CharField(max_length=64, null=True) # works language_code = models.CharField(max_length=2, default='en') # works is_dumb = models.BooleanField(default=False), # doesn't work I had a trailing ',' in one line perhaps from copy&paste. The line with is_dumb doesn't created a model migration with './manage.py makemigrations' but also didn't throw an error. After removing the ',' it worked as expected. So be careful when you do copy&paste :-) It is a comment but should probably be an answer. Make sure that your app name is in settings.py INSTALLED_APPS otherwise no matter what you do it will not run the migrations. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', ] Then run: ./manage.py makemigrations blog There are sometimes when ./manage.py makemigrations is superior to ./manage.py makemigrations because it can handle certain conflicts between apps. Those occasions occur silently and it takes several hours of swearing to understand the real meaning of the dreaded No changes detected message. Therefore, it is a far better choice to make use of the following command: ./manage.py makemigrations ... I had copied a table in from outside of django and the Meta class defaulted to "managed = false". For example: class Rssemailsubscription(models.Model): id = models.CharField(primary_key=True, max_length=36) ... area = models.FloatField('Area (Sq. KM)', null=True) class Meta: managed = False db_table = 'RSSEmailSubscription' By changing managed to True, makemigrations started picking up changes. 1. Make sure your app is mentioned in installed_apps in settings.py 2. Make sure you model class extends models.Model 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. Another possible reason is if you had some models defined in another file (not in a package) and haven't referenced that anywhere else. For me, simply adding from .graph_model import * to admin.py (where graph_model.py was the new file) fixed the problem.