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
Django - makemigrations - No changes detected
提问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 startapp
command 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 migrations
package/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 makemigrations
and specify the app name. The migrations folder will be created.
要为应用程序创建初始迁移,请运行makemigrations
并指定应用程序名称。将创建迁移文件夹。
./manage.py makemigrations <myapp>
Your app must be included in INSTALLED_APPS
first (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.py
file, but created a models
directory and created the my_model.py
file 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__.py
file 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 makemigrations
command.
django 在makemigrations
命令期间没有检测到要迁移的内容有多种可能的原因。
- migration folderYou need a migrations package in your app.
- INSTALLED_APPSYou need your app to be specified in the
INSTALLED_APPS
.dict - Verbositystart by running
makemigrations -v 3
for verbosity. This might shed some light on the problem. - Full pathIn
INSTALLED_APPS
it is recommended to specify the full module app config path 'apply.apps.MyAppConfig' - --settingsyou might want to make sure the correct settings file is set:
manage.py makemigrations --settings mysite.settings
- 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. model metacheck you have the right
app_label
in your model metaDebug 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
)
- 迁移文件夹您的应用程序中需要一个迁移包。
- INSTALLED_APPS您需要在
INSTALLED_APPS
.dict 中指定您的应用 - Verbosity从运行详细开始
makemigrations -v 3
。这可能会说明问题。 - 完整路径在
INSTALLED_APPS
建议指定完整的模块应用程序配置路径“apply.apps.MyAppConfig” - --settings您可能想要确保设置了正确的设置文件:
manage.py makemigrations --settings mysite.settings
- 明确指定应用程序名称将应用程序名称放入其中
manage.py makemigrations myapp
- 这样可以缩小应用程序本身的迁移范围并帮助您隔离问题。 模型元检查你
app_label
在你的模型元中是否有权利调试 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_syncdb
method.
- 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 makemigrations
in some other ways. But to me, the problem was in the Meta
subclass of models.
我读过很多关于这个问题的答案,经常说只是makemigrations
以其他方式运行。但对我来说,问题出在Meta
模型的子类中。
I have an app config that says label = <app name>
(in the apps.py
file, beside models.py
, views.py
etc). 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.py
,views.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_APPS
otherwise 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 makemigrations
is 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 swearing
to understand the real meaning of the dreaded No changes detected
message.
这些场合默默地发生,需要几个小时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
- Make sure your app is mentioned in installed_apps in settings.py
- Make sure you model class extends models.Model
- 确保在 settings.py 中的 installed_apps 中提到了您的应用
- 确保您的模型类扩展了 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”中未检测到任何更改