python - Django 测试错误 : Column does not exist

标签 python django postgresql

Postgresql , Django 2.0 , Python 3.6.4

运行更改字段名称的迁移后 desktop_pay简单地 pay ,运行时出现错误 manage.py test说专栏pay不存在。

这是迁移:

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('instructions', '0055_auto_20180517_1508'),
    ]

    operations = [
        migrations.RenameField(
            model_name='instruction',
            old_name='desktop_pay',
            new_name='pay',
        ),
    ]

这是错误:

> python .\manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column instructions_instruction.pay does not exist
LINE 1: "...on"."quota", "instructions_instruction"."target", "instructi...
                                                              ^

不过,如果我启动 psql 提示符,我可以清楚地看到该列确实存在,至少在“真实”表中是这样。

mydatabase=> \d+ instructions_instruction
                                                              Table "public.instructions_instruction"
       Column        |          Type          | Collation | Nullable |                       Default                        | Storage  | Stats target | Description
---------------------+------------------------+-----------+----------+------------------------------------------------------+----------+--------------+-------------
 id                  | integer                |           | not null | nextval('instructions_instruction_id_seq'::regclass) | plain    |              |
 quota               | smallint               |           | not null |                                                      | plain    |              |
 pay                 | numeric(5,2)           |           | not null |                                                      | main     |              |

### etc...

这是怎么回事?为什么 Django 找不到该列?我该如何调试?

最佳答案

我也遇到过这个问题。首先,我不明白为什么这个错误只在测试时出现。

然后我查看了回溯,发现 Django 无法运行与 Django 无法找到的列无关的我的迁移之一。

它是手动编写的迁移,以使用自动生成的值填充对象 uuid 字段。我像往常一样导入模型 Post

import uuid
from django.db import migrations

from posts.models import Post


def gen_uuid(apps, schema_editor):
    for post in Post.objects.all():
        post.uuid = uuid.uuid4()
        post.save(update_fields=["uuid"])


class Migration(migrations.Migration):

    dependencies = [
        ("posts", "0014_post_uuid"),
    ]

    operations = [
        migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
    ]

我不太清楚为什么,但是这段代码在准备测试数据库时中断了迁移(并且在测试模式之外工作正常)。因此,任何后续迁移都不适用于测试数据库,这会导致 column does not exist 错误。

那怎么办?使用 apps.get_model 而不是官方 Django 文档中关于编写迁移的描述 https://docs.djangoproject.com/en/3.1/howto/writing-migrations/#migrations-that-add-unique-fields .

像这样。

import uuid
from django.db import migrations
    
def gen_uuid(apps, schema_editor):
    Post = apps.get_model("posts", "Post")
    for post in Post.objects.all():
        post.uuid = uuid.uuid4()
        post.save(update_fields=["uuid"])

关于python - Django 测试错误 : Column does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50417251/

相关文章:

python - 如何在 numpy 中创建反对角单位矩阵(其中对角线从左向右翻转)

python - C实现python的len函数解释

Django-Oscar 覆盖模板标签

Django:如何组织这个大模型/经理/设计困惑?

postgresql - Amazon RDS Postgres -> 授予 pg_catalog 权限

postgresql - 如何从 postgresql 中获取 TEXT 列值

c# - 试图在 C# 中理解 Postgres 的二进制复制格式

python - 每当我尝试运行 Google 应用程序引擎开发服务器时,它都会抛出以下错误

python - 如何在 Rodeo 中导入更新的模块而不重新启动 session ?

django - 修改 Wagtail 发布下拉列表(每个应用程序)