我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。

通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。

调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。

如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?


当前回答

我读过很多关于这个问题的答案,通常都是简单地用其他方式进行移民。但对我来说,问题在于模型的Meta子类。

我有一个应用程序配置,说label = <应用程序名称>(在apps.py文件,旁边的models.py, views.py等)。如果你的元类没有与应用标签相同的标签(例如,因为你把一个太大的应用拆分成多个),就不会检测到任何变化(也不会有任何有用的错误消息)。所以在我的模型类中,我现在有:

class ModelClassName(models.Model):

    class Meta:
        app_label = '<app name>' # <-- this label was wrong before.

    field_name = models.FloatField()
    ...

运行Django 1.10。

其他回答

这是一个评论,但可能应该是一个答案。

确保你的应用程序名称在settings.py INSTALLED_APPS中,否则无论你做什么,它都不会运行迁移。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'blog',
]

然后运行:

./manage.py makemigrations blog

django在执行makemigrations命令时没有检测到要迁移的内容有多种可能的原因。

migration folder You need a migrations package in your app. INSTALLED_APPS You need your app to be specified in the INSTALLED_APPS .dict Verbosity start by running makemigrations -v 3 for verbosity. This might shed some light on the problem. Full path In INSTALLED_APPS it is recommended to specify the full module app config path 'apply.apps.MyAppConfig' --settings you might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings 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. model meta check you have the right app_label in your model meta 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)

多个数据库:

当使用django的Db Router时,Router类(你的自定义Router类)需要实现allow_syncdb方法。

Makemigrations总是为模型更改创建迁移,但是如果 allow_migrate()返回False,

另一种可能是您压缩了一些迁移并应用了结果迁移,但忘记从其中删除replace属性。

我这样解决了这个问题:

Erase the "db.sqlite3" file. The issue here is that your current data base will be erased, so you will have to remake it again. Inside the migrations folder of your edited app, erase the last updated file. Remember that the first created file is: "0001_initial.py". For example: I made a new class and register it by the "makemigrations" and "migrate" procedure, now a new file called "0002_auto_etc.py" was created; erase it. Go to the "pycache" folder (inside the migrations folder) and erase the file "0002_auto_etc.pyc". Finally, go to the console and use "python manage.py makemigrations" and "python manage.py migrate".

我知道这是一个老问题,但我一整天都在和这个问题斗争,我的解决方法很简单。

我的目录结构是这样的……

apps/
   app/
      __init__.py
      app_sub1/
           __init__.py
           models.py
      app_sub2/
           __init__.py
           models.py
      app_sub3/
           __init__.py
           models.py
   app2/
      __init__.py
      app2_sub1/
           __init__.py
           models.py
      app2_sub2/
           __init__.py
           models.py
      app2_sub3/
           __init__.py
           models.py
    main_app/
      __init__.py
      models.py

因为所有其他模型,直到我有一个问题,被导入到其他地方,最终从main_app导入,这是在INSTALLED_APPS注册的,我只是很幸运,他们都工作。

但是,由于我只将每个应用程序添加到INSTALLED_APPS,而不是app_sub*,当我最终添加一个新的模型文件时,没有导入到其他任何地方,Django完全忽略了它。

我的修复是添加一个models.py文件到每个应用程序的基本目录,就像这样…

apps/
   app/
      __init__.py
      models.py <<<<<<<<<<--------------------------
      app_sub1/
           __init__.py
           models.py
      app_sub2/
           __init__.py
           models.py
      app_sub3/
           __init__.py
           models.py
   app2/
      __init__.py
      models.py <<<<<<<<<<--------------------------
      app2_sub1/
           __init__.py
           models.py
      app2_sub2/
           __init__.py
           models.py
      app2_sub3/
           __init__.py
           models.py
    main_app/
      __init__.py
      models.py

然后从apps。app中添加。App_sub1 import *,以此类推到每个应用级models.py文件。

在那里……我花了很长时间才弄明白,我在任何地方都找不到解决方案……我甚至翻到了谷歌结果的第二页。

希望这能帮助到一些人!