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

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

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

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


当前回答

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

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

其他回答

我在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, ...)

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

我这样解决了这个问题:

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".

INSTALLED_APPS = [

    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

]

确保“blog.apps”。BlogConfig',(这包含在你的settings.py中,以便进行应用程序迁移)

然后运行python3 manage.py makemigrationblog或你的应用程序名

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,

这可以通过下面提到的两个步骤来完成。

将你的应用添加到settings.py > INSTALLED_APPS 打开admin.py


from .models import upImg
# Register your models here.
admin.site.register(upImg)

注意:将upImg替换为models.py中定义的className

在此之后,查看是否仍然有python manage.py makemigrations。如果有,也执行python manage.py migrate。

更多信息请参考django教程。