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

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

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

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


当前回答

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,

其他回答

在创建一个名为deals的新应用程序时,我遇到了一个不同的问题。我想在应用程序中分离模型,所以我有两个模型文件,分别命名为deals.py和dealers.py。 当运行python manage.py makemigrations时,我得到:未检测到变化。

我继续,在__init__.py中,它位于与我的模型文件(deals和dealer)相同的目录中

from .deals import *
from .dealers import *

然后makemigrations命令起作用了。

事实证明,如果你没有在任何地方导入模型,或者你的模型文件名不是models.py,那么模型就不会被检测到。

发生在我身上的另一个问题是我在settings.py中编写应用程序的方式:

我有:

apps.deals

它应该包括根项目文件夹:

cars.apps.deals

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

我从django外部复制了一个表,Meta类默认为“managed = false”。例如:

class Rssemailsubscription(models.Model):
    id = models.CharField(primary_key=True, max_length=36)
    ...
    area = models.FloatField('Area (Sq. KM)', null=True)

    class Meta:
        managed = False
        db_table = 'RSSEmailSubscription'

通过将managed改为True, makemig开始发生变化。

还有一个非常愚蠢的问题是在模型中定义两个类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")]

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

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