Django 3.0.7 - 对模型进行更改后未检测到任何更改
Django 3.0.7 - No Changes Detected after making Changes to Model
我正在更改模型 AppContactCnt
以添加新字段来处理记录删除。
这是 models.py
class AppContactCnt(models.Model):
id_cnt = models.AutoField(primary_key=True)
# idcst_cnt = models.IntegerField()
idcst_cnt = models.ForeignKey(AppCustomerCst, models.DO_NOTHING, db_column='idcst_cnt')
idctp_cnt = models.ForeignKey(AppContactTypeCtp, models.DO_NOTHING, db_column='idctp_cnt')
idtrp_cnt = models.IntegerField()
name_cnt = models.CharField(max_length=50)
phone_cnt = models.CharField(max_length=50, blank=True, null=True)
email_cnt = models.CharField(max_length=100, blank=True, null=True)
note_cnt = models.CharField(max_length=100, blank=True, null=True)
receives_emails_cnt = models.BooleanField(blank=True, null=True)
deleted_cnt = models.BooleanField(blank=True, null=True)
# date_deleted_cnt = models.DateTimeField(blank=True, null=True)
# deleted_by_cnt = models.CharField(max_length=100, blank=True, null=True)
class Meta:
db_table = 'app_contact_cnt'
app_label = 'easytrade20'
我尝试添加 managed = True
没有变化。
还尝试了以下方法:
删除除init之外的所有之前的迁移文件和pycache文件。
python manage.py migrate --fake-initial
python manage.py makemigrations
python manage.py migrate
我还在No changes detected
我不确定如何进行,欢迎任何帮助。
试试这个:
python manage.py makemigrations [app_name]
python manage.py migrate
您在元选项中设置了 app_label = 'easytrade20'
,但 Django 无法检测到名为 easytrade20
[=24 的应用程序=] 在你的项目中。这就是为什么没有任何变化。
因此,将 easytrade20
添加到您的 INSTALLED_APPS
或删除 app_label = 'easytrade20'
来自模型的 Meta,然后再次尝试 运行 您的迁移。
问题
该应用程序可能未在位于 settings.py 的 INSTALLED_APPS
中列出,因此 Django 尚未注册您的应用程序,并且不会检测需要在该应用程序中 运行 的迁移。
解决方案
将 easytrade20
添加到您的 INSTALLED_APPS
并再次尝试 运行 您的迁移。
如果以上答案对您不起作用,我建议您基于现有数据库制作模型和迁移。完成此任务的步骤概述如下。
Below I suggests that your app directory also called easytrade20
.
I think that you already have a database that you are working with and all migrations have been applied to it. If not, and saving the
structure of migrations is not critical for you, then skip steps 2-3.
If any of your other apps have migrations that depend on easytrade20
migrations you need to repeat steps 2-5 for every of them (by renaming easytrade20
to your app name) before starting step 6 (and 6+ steps are required for that apps too).
0。更改前备份您的文件和数据库
1。将您的 Django 版本更新到最新版本
pip install django==3.0.* -U
for 3.0.X(3.0.10
是我写这些行时的最新版本)
或
pip install django==3.1.*
最新的(3.1 与 3.0 没有太大区别,也许它是您正确的选择)
2。检查是否应用了所有迁移
使用 showmigrations 命令显示应用的迁移:
python manage.py showmigrations
您将看到所有应用程序及其迁移的结构(例如 app1
和 app2
):
app1
[X] 0001_initial
app2
[X] 0001_initial
[X] 0002_some_other_migration
您会看到您的应用程序和一些 django apps/third-party 包,例如 admin
、auth
、database
、sessions
等
如果您在每次迁移附近都能看到所有 [X]
- 一切都很好,请继续。
如果您看到一些 [ ] migration_name
- 请先尝试应用这些迁移。 运行 python manage.py migrate app_name migration_name
对于每个应用程序的每个自上而下的未应用(未检查)迁移(例如 app_name
)。
3。根据您的数据库结构制作新模型
使用inspectdb生成模型:
python manage.py inspectdb > generated_models.py
文件 generated_models.py
将被创建。您需要从此文件复制您为 easytrade20
应用程序创建的所有模型。用生成的模型替换您的 models.py
并至少在模型 AppContactCnt
.
中删除 managed = False
4。从数据库
中删除有关 easytrade20
迁移的信息
python manage.py migrate --fake easytrade20 zero
现在如果您 运行 python manage.py showmigrations easytrade20
您会看到所有迁移都未应用。
5。删除您的迁移目录
就是这样。完全删除目录 easytrade20/migrations
。
然后如果你 运行 python manage.py showmigrations easytrade20
你会看到文字 (no migrations)
6。生成新的迁移
运行 makemigrations 命令:
python manage.py makemigrations easytrade20
文件夹 easytrade20/migrations
将被创建。如果没有,那么您需要检查两件事:
- 您的
settings.py
确保easytrade20
包含在您的INSTALLED_APPS
中。
- 您的
easytrade20
应用设置。确保您的文件具有类似的代码:
# easytrade20/__init__.py
default_app_config = 'easytrade20.apps.EasyTradeConfig'
# easytrade20/apps.py
from django.apps import AppConfig
class EasyTradeConfig(AppConfig):
name = 'easytrade20'
7。应用初始迁移
如果你有这个table的数据库,那么假迁移:
python manage.py migrate easytrade20 --fake-initial
如果没有,那么 运行 没有 --fake-initial
:
python manage.py migrate easytrade20
8。更改您的模型,创建并应用迁移
更改您的 models.py
文件。
运行python manage.py makemigrations easytrade20
。将在您的 easytrade20/migrations
目录中创建新文件。
运行 python manage.py migrate easytrade20
应用新迁移。
确保您的应用已在 settings.py
和 INSTALLED_APP
部分中注册。
我正在更改模型 AppContactCnt
以添加新字段来处理记录删除。
这是 models.py
class AppContactCnt(models.Model):
id_cnt = models.AutoField(primary_key=True)
# idcst_cnt = models.IntegerField()
idcst_cnt = models.ForeignKey(AppCustomerCst, models.DO_NOTHING, db_column='idcst_cnt')
idctp_cnt = models.ForeignKey(AppContactTypeCtp, models.DO_NOTHING, db_column='idctp_cnt')
idtrp_cnt = models.IntegerField()
name_cnt = models.CharField(max_length=50)
phone_cnt = models.CharField(max_length=50, blank=True, null=True)
email_cnt = models.CharField(max_length=100, blank=True, null=True)
note_cnt = models.CharField(max_length=100, blank=True, null=True)
receives_emails_cnt = models.BooleanField(blank=True, null=True)
deleted_cnt = models.BooleanField(blank=True, null=True)
# date_deleted_cnt = models.DateTimeField(blank=True, null=True)
# deleted_by_cnt = models.CharField(max_length=100, blank=True, null=True)
class Meta:
db_table = 'app_contact_cnt'
app_label = 'easytrade20'
我尝试添加 managed = True
没有变化。
还尝试了以下方法: 删除除init之外的所有之前的迁移文件和pycache文件。
python manage.py migrate --fake-initial
python manage.py makemigrations
python manage.py migrate
我还在No changes detected
我不确定如何进行,欢迎任何帮助。
试试这个:
python manage.py makemigrations [app_name]
python manage.py migrate
您在元选项中设置了 app_label = 'easytrade20'
,但 Django 无法检测到名为 easytrade20
[=24 的应用程序=] 在你的项目中。这就是为什么没有任何变化。
因此,将 easytrade20
添加到您的 INSTALLED_APPS
或删除 app_label = 'easytrade20'
来自模型的 Meta,然后再次尝试 运行 您的迁移。
问题
该应用程序可能未在位于 settings.py 的 INSTALLED_APPS
中列出,因此 Django 尚未注册您的应用程序,并且不会检测需要在该应用程序中 运行 的迁移。
解决方案
将 easytrade20
添加到您的 INSTALLED_APPS
并再次尝试 运行 您的迁移。
如果以上答案对您不起作用,我建议您基于现有数据库制作模型和迁移。完成此任务的步骤概述如下。
Below I suggests that your app directory also called
easytrade20
.
I think that you already have a database that you are working with and all migrations have been applied to it. If not, and saving the structure of migrations is not critical for you, then skip steps 2-3.
If any of your other apps have migrations that depend on
easytrade20
migrations you need to repeat steps 2-5 for every of them (by renamingeasytrade20
to your app name) before starting step 6 (and 6+ steps are required for that apps too).
0。更改前备份您的文件和数据库
1。将您的 Django 版本更新到最新版本
pip install django==3.0.* -U
for 3.0.X(3.0.10
是我写这些行时的最新版本)
或
pip install django==3.1.*
最新的(3.1 与 3.0 没有太大区别,也许它是您正确的选择)
2。检查是否应用了所有迁移
使用 showmigrations 命令显示应用的迁移:
python manage.py showmigrations
您将看到所有应用程序及其迁移的结构(例如 app1
和 app2
):
app1
[X] 0001_initial
app2
[X] 0001_initial
[X] 0002_some_other_migration
您会看到您的应用程序和一些 django apps/third-party 包,例如 admin
、auth
、database
、sessions
等
如果您在每次迁移附近都能看到所有 [X]
- 一切都很好,请继续。
如果您看到一些 [ ] migration_name
- 请先尝试应用这些迁移。 运行 python manage.py migrate app_name migration_name
对于每个应用程序的每个自上而下的未应用(未检查)迁移(例如 app_name
)。
3。根据您的数据库结构制作新模型
使用inspectdb生成模型:
python manage.py inspectdb > generated_models.py
文件 generated_models.py
将被创建。您需要从此文件复制您为 easytrade20
应用程序创建的所有模型。用生成的模型替换您的 models.py
并至少在模型 AppContactCnt
.
managed = False
4。从数据库
中删除有关easytrade20
迁移的信息
python manage.py migrate --fake easytrade20 zero
现在如果您 运行 python manage.py showmigrations easytrade20
您会看到所有迁移都未应用。
5。删除您的迁移目录
就是这样。完全删除目录 easytrade20/migrations
。
然后如果你 运行 python manage.py showmigrations easytrade20
你会看到文字 (no migrations)
6。生成新的迁移
运行 makemigrations 命令:
python manage.py makemigrations easytrade20
文件夹 easytrade20/migrations
将被创建。如果没有,那么您需要检查两件事:
- 您的
settings.py
确保easytrade20
包含在您的INSTALLED_APPS
中。 - 您的
easytrade20
应用设置。确保您的文件具有类似的代码:
# easytrade20/__init__.py
default_app_config = 'easytrade20.apps.EasyTradeConfig'
# easytrade20/apps.py
from django.apps import AppConfig
class EasyTradeConfig(AppConfig):
name = 'easytrade20'
7。应用初始迁移
如果你有这个table的数据库,那么假迁移:
python manage.py migrate easytrade20 --fake-initial
如果没有,那么 运行 没有 --fake-initial
:
python manage.py migrate easytrade20
8。更改您的模型,创建并应用迁移
更改您的 models.py
文件。
运行python manage.py makemigrations easytrade20
。将在您的 easytrade20/migrations
目录中创建新文件。
运行 python manage.py migrate easytrade20
应用新迁移。
确保您的应用已在 settings.py
和 INSTALLED_APP
部分中注册。