我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
我试图使用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,
其他回答
我从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开始发生变化。
这可能会帮助其他人,因为我最终花了几个小时试图追踪它。
如果您的模型中有同名的函数,这将删除该值。事后看来很明显,但尽管如此。
所以,如果你有这样的东西:
class Foobar(models.Model):
[...]
something = models.BooleanField(default=False)
[...]
def something(self):
return [some logic]
在这种情况下,该函数将覆盖上面的设置,使其“隐形”进行移民。
这很简单,你需要在空的migrations文件夹中添加空的init.py。 然后使用"python manage.py makemigrations"检查迁移
目录结构,
你的应用 迁移 init.py
对于这个错误,我的问题在于我包含了:
class Meta:
abstract = True
里面的模型,我想创建迁移。
另一个会导致这种情况的是字段后面的尾随逗号,这将导致在makemigrationations期间跳过字段:
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
我有一个拖尾,在一行中,可能来自复制粘贴。带有is_dumb的代码行不会使用./manage.py makemigrations创建模型迁移,因为Python认为它是一个元组,而Django不认为它是一个字段。