makemigrations不检测模型中的变化 [英] makemigrations doesn't detect changes in model

查看:492
本文介绍了makemigrations不检测模型中的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.9.6。我最近删除了我的迁移,并运行 migrate --run-syncdb makemigrations my_app 。今天我向我的一个模型添加了一个新的领域:



models.py:

  value = models.PositiveSmallIntegerField(null = True)

我尝试迁移更改,但 makemigrations 不会检测到更改。它只是开发版本,所以我可以重新同步(我不必保留数据),但运行 - run-syncdb 再次也不会检测到它。



为什么不迁移?

解决方案

删除迁移,您应该压缩他们。如果你只是删除文件,你可能会搞砸事情,最简单的恢复方法是重新同步你的代码,以获取文件。更复杂的路由是从 django_migrations 表中删除所有记录,并从头开始重新初始化迁移,但是还有更多的步骤/问题,我可以真正地进入不要推荐它。



makemigrations没有检测到更改的原因可能是因为该应用程序中没有迁移文件夹。如果您运行 python manage.py makemigrations your_app --initial ,它可能会检测并生成迁移,否则可能会因为文件和 django_migrations 表。



- run-syncdb 命令是伟大的您不关心数据,通常在您实际部署之前,但一旦开始使用迁移,您不应再使用 - run-syncdb 命令。例如,在初始开发过程中,这里的代码是运行每个模型更改,而不是处理迁移:

  dropdb mydb& amp ; createdb mydb&& python manage.py migrate --run-syncdb&& python manage.py loaddata初始

我将所有初始数据存储在一个 fixtures文件,该命令将擦除整个数据库, - 运行-syncdb 重建模式,并在跳过实际迁移文件时加载初始数据。



所以,如果你不在乎任何的数据,或者可以轻松地将其移动到夹具上,而不是放下并重新创建数据库。然后,您可以自由删除所有迁移文件夹,您可以使用上述命令直到您上线,并需要转移到使用迁移。


I'm using django 1.9.6. I recently deleted my migrations and ran migrate --run-syncdb and makemigrations my_app. Today I added a new field to one of my models:

models.py:

value = models.PositiveSmallIntegerField(null=True)

I tried to migrate the changes, but makemigrations doesn't detect the change. It's just the development version, so I can resync (I don't have to preserve the data), but running --run-syncdb again doesn't detect it either.

Why isn't this migrating?

解决方案

You should not delete migrations, you should squash them. If you simply deleted the files you likely messed things up, the easiest way to recover is re-sync your code to get the files back. A more complex route is to delete all the records from the django_migrations table and re-init the migrations from scratch but there is more steps/issues than I can really get into and I don't recommend it.

The reason makemigrations is not detecting the change is likely because there is no migrations folder in that app. If you run python manage.py makemigrations your_app --initial it might detect and generate the migrations or it might freak out because of the difference in your files and the django_migrations table.

The --run-syncdb command is great when you don't care about the data, usually before you actually deploy but once you start using migrations you should not use the --run-syncdb command anymore. For instance, during initial development here is the code I run every model change instead of dealing with migrations:

dropdb mydb && createdb mydb && python manage.py migrate --run-syncdb && python manage.py loaddata initial

I store all the initial data in a fixtures file and the command wipes out the entire database, the --run-syncdb rebuilds the schema, and the initial data is loaded while skipping actual migration files.

So, if you don't care about any of your data or can easily move it to a fixture than you can drop and re-create the DB. You are then free to delete all migration folders and you can use the command above until you go live and need to move to using migrations.

这篇关于makemigrations不检测模型中的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆