添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

model_name='foo', name='bar', field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
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),

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)
                If you want to retain the same name, all you have to do is comment the field, then do a makemigrations + migrate to completely remove the field, then uncomment  that same field and add the modifications you'll like to do, then do another makemigrations + migrate.
– Eyong Kevin Enowanyo
                Nov 18, 2021 at 10:14
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.