Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.ProgrammingError: cannot cast type text[] to jsonb

I was trying to include JSONField in my model:

from django.contrib.postgres.fields import JSONField
class Trigger(models.Model):
    solutions = JSONField(blank=True, null=True)

However, when I try to migrate the database, it gives the following error:

django.db.utils.ProgrammingError: cannot cast type text[] to jsonb
LINE 1: ...ALTER COLUMN "solutions" TYPE jsonb USING "solutions"::jsonb

What could be done here?

like image 997
QuestionEverything Avatar asked Jul 16 '17 22:07

QuestionEverything


2 Answers

You can update the migration file from

operations = [
    migrations.AlterField(
        model_name='foo',
        name='bar',
        field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
    ),
]

to

operations = [
    migrations.RemoveField(
        model_name='foo',
        name='bar',
    ),
    migrations.AddField(
        model_name='foo',
        name='bar',
        field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
    ),
]
like image 101
Django Doctor Avatar answered Oct 19 '22 03:10

Django Doctor


Error shows that you are trying to alter column and not add a new one. This column solutions seems to be declared as a Textfield (or Charfield) previously with data in it, which you are trying to convert to JSON field. That's why you are getting this error.

Better create a new field rather than altering a text field to JSON field and remove the previous field, if that is unnecessary.

from django.contrib.postgres.fields import JSONField

class Trigger(models.Model):
    new_solutions = JSONField(blank=True, null=True)
like image 41
Raj Subit Avatar answered Oct 19 '22 04:10

Raj Subit