将 psqlextra.backend 与 django 一起使用时无法将数据插入 ArrayField
Not able to insert data into ArrayField while using psqlextra.backend with django
我有一个带有 ArrayField 的模型,以 JSONField 作为基础。当我尝试插入数据时,出现以下错误:
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.
我正在使用 psqlextra.backend 作为我的默认数据库引擎。我的模型和其他相关代码是:
from django.db import models
from django.contrib.postgres.fields import JSONField, ArrayField
class TestModel(models.Model):
data = ArrayField(
JSONField(),
blank=True,
)
我对应的迁移文件如下所示:
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),
),
]
我试过这样添加数据:
TestModel.objects.create(
data= [{"a":"b"}, {"c":"d"}]
)
进一步挖掘后,这是 Django<2.2 中的一个已知错误
https://code.djangoproject.com/ticket/28291
升级 django 成功了!
我有一个带有 ArrayField 的模型,以 JSONField 作为基础。当我尝试插入数据时,出现以下错误:
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.
我正在使用 psqlextra.backend 作为我的默认数据库引擎。我的模型和其他相关代码是:
from django.db import models
from django.contrib.postgres.fields import JSONField, ArrayField
class TestModel(models.Model):
data = ArrayField(
JSONField(),
blank=True,
)
我对应的迁移文件如下所示:
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),
),
]
我试过这样添加数据:
TestModel.objects.create(
data= [{"a":"b"}, {"c":"d"}]
)
进一步挖掘后,这是 Django<2.2 中的一个已知错误
https://code.djangoproject.com/ticket/28291
升级 django 成功了!