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

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

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

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


当前回答

确保你的应用在settings.py中的installed_apps中被提及 确保建模类扩展了模型。模型

其他回答

解决方案是你必须把你的应用包含在INSTALLED_APPS中。

我错过了它,我发现了同样的问题。

在指定我的应用程序名称迁移成功

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

请注意,我在最后提到了boards,这是我的应用程序名称。

我这样解决了这个问题:

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

还有一个边缘情况和解决方案:

我添加了一个布尔字段,同时添加了一个@属性引用它,具有相同的名称(doh)。注释属性和迁移看到并添加新字段。重新命名财产,一切都很好。

另一个会导致这种情况的是字段后面的尾随逗号,这将导致在makemigrationations期间跳过字段:

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

我有一个拖尾,在一行中,可能来自复制粘贴。带有is_dumb的代码行不会使用./manage.py makemigrations创建模型迁移,因为Python认为它是一个元组,而Django不认为它是一个字段。

我的问题比上面的答案要简单得多,可能是一个更常见的原因,只要你的项目已经设置好并正在工作。在我的一个已经工作了很长时间的应用程序中,迁移似乎不稳定,所以我匆忙地做了以下工作:

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

Whaat ? ?

我还错误地删除了所有__init__.py文件:(-在我进入后,一切都重新工作了:

touch ads1/migrations/__init__.py

对于我的每一份申请,makemigrationations都再次起作用。

原来我通过复制另一个应用程序手动创建了一个新的应用程序,并且忘记将__init__.py放在migrations文件夹中,这限制了我,使一切都不稳定-导致我使用上面描述的rm -r使情况更糟。

希望这能帮助一些人在“未检测到更改”的错误中咒骂几个小时。