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

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

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

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


当前回答

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

其他回答

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或你的应用程序名

还有一个非常愚蠢的问题是在模型中定义两个类Meta。在这种情况下,在运行makemigrations时,对第一个的任何更改都不会应用。

class Product(models.Model):
    somefield = models.CharField(max_length=255)
    someotherfield = models.CharField(max_length=255)

    class Meta:
        indexes = [models.Index(fields=["somefield"], name="somefield_idx")]

    def somefunc(self):
        pass

    # Many lines...

    class Meta:
        indexes = [models.Index(fields=["someotherfield"], name="someotherfield_idx")]

更新:在尝试之前,应该确保migrations文件夹中存在__init__.py文件:

./manage.py makemigrations <myapp1> <myapp2>…< myappN >


有时。/manage.py makemigrations优于。/manage.py makemigrations <myapp>,因为它可以处理应用程序之间的某些冲突。

这些情况都是悄无声息地发生的,需要几个小时的咒骂才能理解可怕的“检测到没有变化”消息的真正含义。

因此,使用下面的命令是一个更好的选择:

./manage.py makemigrations <myapp1> <myapp2>…< myappN >

你应该将polls.apps.PollsConfig添加到setting.py中的INSTALLED_APPS中

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