首页 > 解决方案 > 为什么当其他文件在 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()

标签: pythondjangopostgresqlherokudeployment

解决方案


感谢@AbdulAzizBarkat,我发现问题并不像我最初想的那样在应用程序的编译级别。在迁移模型之前,我对模型进行了代码修改/引用。我尝试了readyAppConfig 的方法,但这对我不起作用。相反,我进行了数据库初始化并编写了一个迁移操作,并且效果非常好。


推荐阅读