我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
当前回答
我这样解决了这个问题:
Erase the "db.sqlite3" file. The issue here is that your current data base will be erased, so you will have to remake it again. Inside the migrations folder of your edited app, erase the last updated file. Remember that the first created file is: "0001_initial.py". For example: I made a new class and register it by the "makemigrations" and "migrate" procedure, now a new file called "0002_auto_etc.py" was created; erase it. Go to the "pycache" folder (inside the migrations folder) and erase the file "0002_auto_etc.pyc". Finally, go to the console and use "python manage.py makemigrations" and "python manage.py migrate".
其他回答
我在django 3.0中遇到了类似的问题,根据官方文档中的迁移部分,运行这个足够更新我的表结构:
python manage.py makemigrations
python manage.py migrate
但是输出总是一样的:在我执行'makemigrations'脚本后,我的模型'未检测到变化'。 我在models.py的模型上有一个语法错误,我想在db上更新:
field_model : models.CharField(max_length=255, ...)
而不是:
field_model = models.CharField(max_length=255, ...)
解决了这个愚蠢的错误,有了这些命令,迁移就没有问题了。也许这能帮到别人。
可能的原因是删除了现有的db文件和migrations文件夹 你可以使用python manage.py makemigrations <app_name>这应该可以工作。我曾经遇到过类似的问题。
还有一个非常愚蠢的问题是在模型中定义两个类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")]
要为应用程序创建初始迁移,请运行makemigrations并指定应用程序名称。迁移文件夹将被创建。
./manage.py makemigrations <myapp>
你的应用必须首先包含在INSTALLED_APPS中(在settings.py内部)。
在我的情况下,我忘记插入类参数
错误的:
class AccountInformation():
正确的
class AccountInformation(models.Model):