2

I have initial migration setup, but I want to change a column from sqlalchemy.Text to sqlalchemy.JSON

I followed this article https://amercader.net/blog/beware-of-json-fields-in-sqlalchemy/

column_foo = Column(mutable_json_type(dbtype=JSONB, nested=True), nullable=True)

When I run alembic autogenerate it does not recognise any change, so I wrote it manually:

import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB
...

def upgrade() -> None:
    op.alter_column("some_table", "column_foo", existing_type=JSONB(), nullable=True)


def downgrade() -> None:
    op.alter_column("some_table", "column_foo", existing_type=sa.TEXT(), nullable=True)

NOTE: I also tried regular sqlalchemy.JSON in my ORM module, and in ALembic migriaon script.

#orm script
column_foo = Column(JSON, nullable=True)

#migration script
op.alter_column("some_table", "column_foo", existing_type=sa.JSON(), nullable=True)

In both scenarios when I check metadata in my postgres database I still see TEXT type:

CREATE TABLE public.some_table (
...
column_foo text NULL,

Why does it not say JSON as a column type?

mehekek
  • 113
  • 9

1 Answers1

3

Found a solution (postgresql_using):

op.alter_column("some_table", "column_foo", type_=sa.JSON(), nullable=True,postgresql_using='column_foo::json')
mehekek
  • 113
  • 9