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

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

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

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


当前回答

好吧,我相信你还没有设置模型,那么它现在迁移了什么?

所以解决方案是设置所有变量和设置Charfield, Textfield.......然后迁移它们,就会成功。

其他回答

你能做的最好的事情是,删除现有的数据库。在我的情况下,我使用phpMyAdmin SQL数据库,所以我手动删除创建的数据库在那里。

删除后: 我在PhpMyAdmin创建数据库,不添加任何表。

再次执行以下命令:

Python manage.py makemigrations

Python manage.py迁移

在这些命令之后:你可以看到django已经自动在数据库中创建了其他必要的表(大约有10个表)。

Python manage.py makemigrations <app_name>

Python manage.py迁移

最后:在以上命令之后,您所创建的所有模型(表)都直接导入到数据库中。

希望这能有所帮助。

我的问题(以及解决方案)与上面描述的不同。

我没有使用models.py文件,而是创建了一个models目录,并在那里创建了my_model.py文件,我把我的模型放在那里。Django找不到我的模型,所以它写道没有迁移可以应用。

我的解决方案是:在my_app/models/__init__.py文件中,我添加了这一行: 导入MyModel

我在django 3.0中遇到了类似的问题,根据官方文档中的迁移部分,运行这个足够更新我的表结构:

python manage.py makemigrations
python manage.py migrate

但是输出总是一样的:在我执行'makemigrations'脚本后,我的模型'未检测到变化'。 我在models.py的模型上有一个语法错误,我想在db上更新:

field_model : models.CharField(max_length=255, ...)

而不是:

field_model = models.CharField(max_length=255, ...)

解决了这个愚蠢的错误,有了这些命令,迁移就没有问题了。也许这能帮到别人。

试着在admin.py中注册你的模型,这里有一个例子:- admin.site.register (YourModelHere)

您可以做以下事情:- 1. admin.site.register(YourModelHere) # 2. 重新加载页面并重试 3.点击CTRL-S并保存 4. 可能有错误,特别检查models.py和admin.py 5. 或者,在结束时重新启动服务器

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,