简体   繁体   English

将 psqlextra.backend 与 django 一起使用时无法将数据插入 ArrayField

[英]Not able to insert data into ArrayField while using psqlextra.backend with django

I have a model with an ArrayField, having JSONField as base.我有一个带有 ArrayField 的 model,以 JSONField 为基础。 When I try to insert data, it gives the following error:当我尝试插入数据时,出现以下错误:

ProgrammingError: column "data" is of type jsonb[] but expression is of type text[]
LINE 1: ...e_status_code", "is_success", "error") VALUES (1, ARRAY['"a"...
                                                             ^
HINT:  You will need to rewrite or cast the expression.

I am using psqlextra.backend as my default database engine.我使用 psqlextra.backend 作为我的默认数据库引擎。 My model and other relevant codes are:我的 model 和其他相关代码是:

from django.db import models
from django.contrib.postgres.fields import JSONField, ArrayField

class TestModel(models.Model):
    data = ArrayField(
        JSONField(),
        blank=True,
        )

My corresponding migration file looks like this:我相应的迁移文件如下所示:

import django.contrib.postgres.fields
import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('emails', '0003_auto_20200408_1351'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='TestModel',
            name='data',
        ),
        migrations.AddField(
            model_name='TestModel',
            name='data',
            field=django.contrib.postgres.fields.ArrayField(base_field=django.contrib.postgres.fields.jsonb.JSONField(), blank=True, size=None),
        ),
    ]

I tried adding data like this:我尝试添加这样的数据:

TestModel.objects.create(
    data= [{"a":"b"}, {"c":"d"}]
    )

After further digging up, this was a known bug in Django<2.2经过进一步挖掘,这是 Django<2.2 中的一个已知错误

https://code.djangoproject.com/ticket/28291 https://code.djangoproject.com/ticket/28291

Upgrading django did the job!升级 django 就可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM