我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
当前回答
还有一个非常愚蠢的问题是在模型中定义两个类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")]
其他回答
另一种可能是您压缩了一些迁移并应用了结果迁移,但忘记从其中删除replace属性。
当向django api应用程序添加新模型并运行python manage.py makemigrations时,工具没有检测到任何新模型。
奇怪的是,旧模型确实被makemigrations选中了,但这是因为它们在urlpatterns链中被引用,而工具以某种方式检测到了它们。所以要注意这种行为。
这个问题是因为与models包对应的目录结构有子包,并且所有__init__.py文件都是空的。它们必须显式地在每个子文件夹和__init__.py模型中导入所有必需的类,以便Django使用makemigrationations工具来获取它们。
models
├── __init__.py <--- empty
├── patient
│ ├── __init__.py <--- empty
│ ├── breed.py
│ └── ...
├── timeline
│ ├── __init__.py <-- empty
│ ├── event.py
│ └── ...
我的问题(以及解决方案)与上面描述的不同。
我没有使用models.py文件,而是创建了一个models目录,并在那里创建了my_model.py文件,我把我的模型放在那里。Django找不到我的模型,所以它写道没有迁移可以应用。
我的解决方案是:在my_app/models/__init__.py文件中,我添加了这一行: 导入MyModel
解决方案是你必须把你的应用包含在INSTALLED_APPS中。
我错过了它,我发现了同样的问题。
在指定我的应用程序名称迁移成功
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boards',
]
请注意,我在最后提到了boards,这是我的应用程序名称。
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,