jsonb[] error

91 views
Skip to first unread message

Scott Ming

unread,
Aug 24, 2019, 11:33:51 PM8/24/19
to peewee-orm
I have a table like this:


```
create table card_exams
(
id bigserial not null
constraint card_exams_pkey
primary key,
info jsonb[] not null,
card_id uuid not null
constraint card_exams_card_id_fkey
references cards,
user_id uuid not null
constraint card_exams_user_id_fkey
references users,
inserted_at timestamp with time zone not null,
updated_at timestamp with time zone not null
);

```

and a peewee model like this:

```python
class CardExams(BaseModel):
    card = ForeignKeyField(column_name='card_id', field='id', model=Cards)
    id = BigAutoField()
    # info = UnknownField(constraints=[SQL("DEFAULT ARRAY[]::jsonb[]")])  # ARRAY
    info = ArrayField(BinaryJSONField)
    inserted_at = DateTimeField()
    updated_at = DateTimeField()
    user = ForeignKeyField(column_name='user_id', field='id', model=Users)

    class Meta:
        table_name = 'card_exams
```

and when I insert this data:

```phthon
data = {'card_id': '00440205-a4b3-4590-80b0-3142f24c240b', 
'inserted_at': datetime.datetime(2019, 8, 2, 11, 24, 10, 698754, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=480, name=None)), 
'updated_at': datetime.datetime(2019, 8, 2, 11, 24, 10, 698780, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=480, name=None)), 
'info': ['{"answers": [[" 学术论文", " 知识图谱", " 社会引用", " 了解大牛生平"]], "question": "```MQ\\n\\n\\nQ: 本节视频用了哪些方法了解平克的关键信息?(多选)\\n\\n\\nASWT: 了解大牛生平\\n\\nASWT: 学术论文\\n\\nASWT: 知识图谱\\n\\nASWT: 社会引用\\n\\n\\n```", "is_correct": true}'], 'user_id': '27228430-fb14-11e6-82e4-210703b60b69'}
```

with this code: 

```python
CardExams.create(**data)
```

will raise an error:

```
peewee.ProgrammingError: column "info" is of type jsonb[] but expression is of type text[]
LINE 1: ...) VALUES ('00440205-a4b3-4590-80b0-3142f24c240b', ARRAY['{"a...
                                                             ^
HINT:  You will need to rewrite or cast the expression.
```

How do I fix it?


J P

unread,
Aug 25, 2019, 5:06:35 AM8/25/19
to peewee-orm
What DB are you using? What version?

Please post the query output. Use insert instead of create to get the SQL for the operation. `query.sql()` where `query` is what you get from `insert`.


http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert

Charles Leifer

unread,
Aug 25, 2019, 8:01:53 AM8/25/19
to peewe...@googlegroups.com
Try initializing the ArrayField with "convert_values=True":

info = ArrayField(BinaryJSONField, convert_values=True)

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/344debd3-6461-42d8-8776-5401e61d97c4%40googlegroups.com.

Charles Leifer

unread,
Aug 25, 2019, 8:03:45 AM8/25/19
to peewe...@googlegroups.com
I'll just add that having an array of JSON fields strikes me as stupid, since json supports arrays already if your top-level value is a list-type.

So maybe just use a regular binaryjson field and treat the top-level value as a list?

Scott Ming

unread,
Aug 25, 2019, 8:48:25 AM8/25/19
to peewe...@googlegroups.com
Yes, you're right, It's stupid, I'll take your advice only use jsonb.

Reply all
Reply to author
Forward
0 new messages