python - 为什么当其他文件在 Heroku 中使用我的模型时我无法迁移?
问题描述
对于上下文,在对数据库进行了一些摆弄之后,我有一个在本地运行良好的应用程序。它使用一个反应前端,一个用于内部 api 调用的 django 后端。但是,在使用新数据库的全新下载时,我总是收到一条错误消息relation "claims" does not exist
,其中 Claims 是我的数据库中表的名称,采用模型的形式。
我已经能够通过注释掉所有使用模型的代码来绕过这个错误,然后慢慢迁移每个表,一次一个,然后取消注释掉使用模型的代码。
这是非常乏味的,但它适用于本地环境。但是,当我尝试将其投入生产时,我正在使用 Heroku,并且无法以相同的方式进行多次提交,因为似乎每次有新更新时数据库都会重置。
我错过了什么吗?我觉得好像我在这里做错了什么,但是除了说要重置我的所有迁移和重做 makemigrations 命令的帖子之外,我找不到其他任何东西。这种方法在 Heroku 中对我不起作用,因为我需要单独进行每个迁移,正如我之前所说的。
作为参考,这里是我的相关文件:models.py
class Claims(models.Model):
id = models.BigAutoField(primary_key=True)
refId = models.CharField(db_column='refId', unique=True, max_length=10) # Field name made lowercase.
title = models.CharField(max_length=20, blank=True, null=True)
description = models.CharField(max_length=256, blank=True, null=True)
image = models.CharField(max_length=70, blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
start = models.DateTimeField(blank=True, null=True)
end = models.DateTimeField(blank=True, null=True)
distributor = models.CharField(max_length=20, blank=True, null=True)
status = models.TextField(blank=True, null=True, choices=claimStatus.choices, default=claimStatus.active) # This field type is a guess.
class Meta:
managed = True
db_table = 'claims'
unique_together = (('id', 'refId'),)
迁移/0001_initial.py
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Claims',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('refId', models.CharField(db_column='refId', max_length=10, unique=True)),
('title', models.CharField(blank=True, max_length=20, null=True)),
('description', models.CharField(blank=True, max_length=256, null=True)),
('image', models.CharField(blank=True, max_length=70, null=True)),
('created_at', models.DateTimeField(blank=True, null=True)),
('start', models.DateTimeField(blank=True, null=True)),
('end', models.DateTimeField(blank=True, null=True)),
('distributor', models.CharField(blank=True, max_length=20, null=True)),
('status', models.TextField(blank=True, choices=[('ACTIVE', 'Active'), ('INACTIVE', 'Inactive'), ('DISCONTINUED', 'Discontinued')], default='ACTIVE', null=True)),
],
options={
'db_table': 'claims',
'managed': True,
'unique_together': {('id', 'refId')},
},
),
]
useClaim.py
from .models import *
claim, created = Claims.objects.get_or_create(
refId = "DROPTEST",
title = "DROPTEST",
description ="DROPTEST",
image = "https://ipfs.io/ipfs/QmcPjQ7iaZ8exuZnH1awHZtkQPyL3TRT1S46wVJxwucay5",
created_at = None,
start = None,
end = None,
distributor = "DROPTEST",
status = claimStatus.active
)
claim.save()
解决方案
感谢@AbdulAzizBarkat,我发现问题并不像我最初想的那样在应用程序的编译级别。在迁移模型之前,我对模型进行了代码修改/引用。我尝试了ready
AppConfig 的方法,但这对我不起作用。相反,我进行了数据库初始化并编写了一个迁移操作,并且效果非常好。
推荐阅读
- printing - iOS 13 AirPrint 问题:可以打印一次到 UIPrinter,随后出现控制台错误,例如 Print-Job failed: Printer not found
- c# - " "这里不允许,因为它没有扩展类 'System.Web.UI.Page'
- json - 在 IE 中将 JSON 显示为 HTML 而无需下载
- html - 我无法通过打开 HTML 文件看到我的 D3.js 设计
- sql - 多张同形桌子的ERD设计
- go - 如何临时从基础 github repo 导入到它的 fork?
- c - TCP IP 标头格式错误的数据包
- django - 我在管理站点中创建了自定义按钮并单击它转到查看 get_changeform_initial_data() 函数并在表单中放置一些静态值
- java - 如何以 JSON 格式返回信息
- codeigniter - 如何在视图解析器中访问循环项的属性?