我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
当前回答
另一个会导致这种情况的是字段后面的尾随逗号,这将导致在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不认为它是一个字段。
其他回答
我这样解决了这个问题:
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".
另一种可能是您压缩了一些迁移并应用了结果迁移,但忘记从其中删除replace属性。
操作方法:
1 .
确保你的应用必须包含在settings.py中的INSTALLED_APPS中
零食 : 2
python manage.py makemigrations <appname>
如果相同的消息显示(未检测到更改)
这对你的项目来说是非常危险的,所以在应用这个方法之前,请确保你的项目有备份。
方法2
重命名您的应用程序名称,并使用:
django-admin startapp <appname>
复制除旧应用程序之外的所有.py文件
迁移文件夹 pycache文件夹 init.py Test.py文件,如果您没有在其中编写代码
并粘贴到你最近制作的新应用程序中
记住,你必须为新应用程序创建完全相同的名称,否则你必须在项目中进行更多更改。
还有一个非常愚蠢的问题是在模型中定义两个类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")]
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,