Python Django - makemigrations - 未检测到更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36153748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:29:05  来源:igfitidea点击:

Django - makemigrations - No changes detected

pythondjangodjango-migrations

提问by Dilraj

I was trying to create migrations within an existing app using the makemigrations command but it outputs "No changes detected".

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

Usually I create new apps using the startappcommand but did not use it for this app when I created it.

通常我使用该startapp命令创建新应用程序,但在创建该应用程序时并未将其用于该应用程序。

After debugging, I found that it is not creating migration because the migrationspackage/folder is missing from an app.

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

Would it be better if it creates the folder if it is not there or am I missing something?

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

回答by Alasdair

To create initial migrations for an app, run makemigrationsand specify the app name. The migrations folder will be created.

要为应用程序创建初始迁移,请运行makemigrations并指定应用程序名称。将创建迁移文件夹。

./manage.py makemigrations <myapp>

Your app must be included in INSTALLED_APPSfirst (inside settings.py).

您的应用程序必须首先包含在INSTALLED_APPS(在 settings.py 中)。

回答by Karina Klinkevi?iūt?

My problem (and so solution) was yet different from those described above.

我的问题(以及解决方案)与上述问题不同。

I wasn't using models.pyfile, but created a modelsdirectory and created the my_model.pyfile there, where I put my model. Django couldn't find my model so it wrote that there are no migrations to apply.

我没有使用models.py文件,而是创建了一个models目录并在my_model.py那里创建了文件,我在其中放置了模型。Django 找不到我的模型,所以它写道没有要应用的迁移。

My solution was: in the my_app/models/__init__.pyfile I added this line: from .my_model import MyModel

我的解决方案是:在my_app/models/__init__.py文件中我添加了这一行: from .my_model import MyModel

回答by user1134422

There are multiple possible reasons for django not detecting what to migrate during the makemigrationscommand.

django 在makemigrations命令期间没有检测到要迁移的内容有多种可能的原因。

  1. migration folderYou need a migrations package in your app.
  2. INSTALLED_APPSYou need your app to be specified in the INSTALLED_APPS.dict
  3. Verbositystart by running makemigrations -v 3for verbosity. This might shed some light on the problem.
  4. Full pathIn INSTALLED_APPSit is recommended to specify the full module app config path 'apply.apps.MyAppConfig'
  5. --settingsyou might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings
  6. specify app nameexplicitly put the app name in manage.py makemigrations myapp- that narrows down the migrations for the app alone and helps you isolate the problem.
  7. model metacheck you have the right app_labelin your model meta

  8. Debug djangodebug 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)

  1. 迁移文件夹您的应用程序中需要一个迁移包。
  2. INSTALLED_APPS您需要在INSTALLED_APPS.dict 中指定您的应用
  3. Verbosity从运行详细开始makemigrations -v 3。这可能会说明问题。
  4. 完整路径INSTALLED_APPS建议指定完整的模块应用程序配置路径“apply.apps.MyAppConfig”
  5. --settings您可能想要确保设置了正确的设置文件:manage.py makemigrations --settings mysite.settings
  6. 明确指定应用程序名称将应用程序名称放入其中manage.py makemigrations myapp- 这样可以缩小应用程序本身的迁移范围并帮助您隔离问题。
  7. 模型元检查你app_label在你的模型元中是否有权利

  8. 调试 django调试 django 核心脚本。makemigrations 命令非常简单。以下是如何在 pycharm 中执行此操作。相应地改变你的脚本定义(例如:makemigrations --traceback myapp

Multiple databases:

多个数据库:

  • Db Routerwhen working with django db router, the router class (your custom router class) needs to implement the allow_syncdbmethod.
  • DB路由器时使用Django DB路由器,路由器类(您的自定义类路由器)工作需要实现的allow_syncdb方法。

makemigrations always creates migrations for model changes, but if allow_migrate() returns False,

makemigrations 总是为模型更改创建迁移,但如果 allow_migrate() 返回 False,

回答by onekiloparsec

I've read many answers to this question often stating to simply run makemigrationsin some other ways. But to me, the problem was in the Metasubclass of models.

我读过很多关于这个问题的答案,经常说只是makemigrations以其他方式运行。但对我来说,问题出在Meta模型的子类中。

I have an app config that says label = <app name>(in the apps.pyfile, beside models.py, views.pyetc). If by any chance your meta class doesn't have the same label as the app label (for instance because you are splitting one too big app into multiple ones), no changes are detected (and no helpful error message whatsoever). So in my model class I have now:

我有一个应用程序的配置,说label = <app name>(中apps.py文件,旁边models.pyviews.py等)。如果您的元类与应用程序标签具有不同的标签(例如,因为您将一个太大的应用程序拆分为多个应用程序),则不会检测到任何更改(并且没有任何有用的错误消息)。所以在我的模型类中,我现在有:

class ModelClassName(models.Model):

    class Meta:
        app_label = '<app name>' # <-- this label was wrong before.

    field_name = models.FloatField()
    ...

Running Django 1.10 here.

在此处运行 Django 1.10。

回答by surfer190

It is a comment but should probably be an answer.

这是一个评论,但可能应该是一个答案。

Make sure that your app name is in settings.py INSTALLED_APPSotherwise no matter what you do it will not run the migrations.

确保您的应用程序名称在 settings.py 中,INSTALLED_APPS否则无论您做什么都不会运行迁移。

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

    'blog',
]

Then run:

然后运行:

./manage.py makemigrations blog

回答by raratiru

There are sometimes when ./manage.py makemigrationsis superior to ./manage.py makemigrations <myapp>because it can handle certain conflicts between apps.

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

Those occasions occur silently and it takes several hours of swearingto understand the real meaning of the dreaded No changes detectedmessage.

这些场合默默地发生,需要几个小时swearing才能理解可怕No changes detected信息的真正含义。

Therefore, it is a far better choice to make use of the following command:

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

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

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

回答by yvess

I had another problem not described here, which drove me nuts.

我还有一个这里没有描述的问题,这让我发疯了。

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

I had a trailing ',' in one line perhaps from copy&paste. The line with is_dumb doesn't created a model migration with './manage.py makemigrations' but also didn't throw an error. After removing the ',' it worked as expected.

我在一行中有一个尾随的 ',' 可能来自复制和粘贴。带有 is_dumb 的行没有使用 './manage.py makemigrations' 创建模型迁移,但也没有抛出错误。删除“,”后,它按预期工作。

So be careful when you do copy&paste :-)

所以在复制和粘贴时要小心:-)

回答by Dan Cogswell

I had copied a table in from outside of django and the Meta class defaulted to "managed = false". For example:

我从 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'

By changing manged to True, makemigrations started picking up changes.

通过将 manged 更改为 True,makemigrations 开始接受更改。

回答by Amandeep Singh

  1. Make sure your app is mentioned in installed_apps in settings.py
  2. Make sure you model class extends models.Model
  1. 确保在 settings.py 中的 installed_apps 中提到了您的应用
  2. 确保您的模型类扩展了 models.Model

回答by CodeToLife

I forgot to put correct arguments:

我忘了提出正确的论点:

class LineInOffice(models.Model):   # here
    addressOfOffice = models.CharField("Корхоная жош",max_length= 200)   #and here
    ...

in models.py and then it started to drop that annoying

在models.py中,然后它开始变得烦人

No changes detected in app 'myApp '

在应用程序“myApp”中未检测到任何更改