Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did I get an error when running migration, I tried to change typedatas text to JSONB (PosgreesSql+sequelize)

I got an error when I was running a migration in sequelize, before I set typedatas text and i tried to change to json I got some error...

ERROR: column "value" cannot be cast automatically to type jsonb

This is the new code:

  up: (queryInterface, Sequelize) => Promise.resolve()
    .then(async () => {
      await queryInterface.changeColumn('Action', 'value', {
        type: Sequelize.JSONB,
        allowNull: false,
        defaultValue: {},
      })
    }),

and this is my migration code that I want to change:

 up: (queryInterface, Sequelize) => queryInterface.createTable('Action', {
    ....
    value: {
      type: Sequelize.TEXT,
      allowNull: false,
      defaultValue: '',
    },
    ....
}

What's wrong?

like image 784
M ilham Avatar asked Sep 02 '25 07:09

M ilham


2 Answers

the accepted answer doesn't work for me.

I use this one and it works.

type: `${Sequelize.JSONB} using to_jsonb(col_name)`
like image 148
Luke Avatar answered Sep 04 '25 22:09

Luke


simple solution... i change to

type: 'JSONB USING CAST ("value" as JSONB)'`

as in https://github.com/sequelize/sequelize/issues/2471

solved my problem ...

like image 22
M ilham Avatar answered Sep 04 '25 20:09

M ilham