Check if table column exists in database using SQLAlchemy and Alembic

I use Alembic as a migration tool and I run the following pseudo script in an already updated database (there are no revision records for Alembic, the database schema is just updated).

revision = '1067fd2d11c8'
down_revision = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    op.add_column('box', sa.Column('has_data', sa.Boolean, server_default='0'))


def downgrade():
    pass

This gives me the following error only for PostgreSQL (everything is fine with MySQL):

INFO  [alembic.migration] Context impl PostgresqlImpl.
INFO  [alembic.migration] Will assume transactional DDL.
INFO  [root] (ProgrammingError) ERREUR:  la colonne « has_data » de la relation « box » existe déjà

The last row means the column has_dataalready exists.

I want to check that the column exists before op.add_column.

+2
source share
1 answer

- . , Alembic . .

, , alembic revision --autogenerate -m "base". alembic stamp head, , , .

+3

All Articles