cannot cast type character varying[] to jsonb django migration

旧街凉风 提交于 2019-12-25 01:33:21

问题


I have alter my model from arrayfield to json field now i am receiving error

cannot cast type character varying[] to jsonb LINE 1: ...LUMN "questionaires" TYPE jsonb USING "questionaires"::jsonb.

How to fix this ? and how it did occur?

from

questionaires   = ArrayField(models.CharField(max_length=4000), null=True, blank=True)

to

questionaires   = JSONField(null = True,blank = True)

回答1:


I think you have to perform multiple stage migrates:

  1. Create temporary new field with type JSONField: Ex questionaires_2
  2. Write custom python migration RunPython to convert data manually
  3. Delete old field: questionaires
  4. Rename temporary field to the old one questionaires_2 -> questionaires

More advance: refer to this https://stackoverflow.com/a/21997589/533738

Not tested!!!

ALTER TABLE <Your Table>
ALTER COLUMN questionaires TYPE JSONB
      USING translate(questionaires::text, '{}','[]')::JSONB;


来源:https://stackoverflow.com/questions/56250393/cannot-cast-type-character-varying-to-jsonb-django-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!