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

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

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

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


当前回答

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

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使情况更糟。

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

其他回答

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

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


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

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

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

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

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,

我从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开始发生变化。

这很简单,你需要在空的migrations文件夹中添加空的init.py。 然后使用"python manage.py makemigrations"检查迁移

目录结构,

你的应用 迁移 init.py

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

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