Search code examples
pythonsqlalchemy

alembic and sqlalchemy server default problem


I have a table structure as such, omitting some columns for clearance:

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
import sqlmodel

        op.create_table('global_evg_report_history',
    sa.Column('issue_status', sa.String(), server_default=sa.text('wrongly_closed'), nullable=True),
        sa.PrimaryKeyConstraint('plada_service_alias', 'run_date', 'technology_version'),
        sa.UniqueConstraint('plada_service_alias', 'technology_version', 'issue_id', 'run_date')
        )

But after trying a migration with alembic I get the following error:

sqlalchemy.exc.NotSupportedError: (psycopg2.errors.FeatureNotSupported) cannot use column reference in DEFAULT expression
issue_status VARCHAR DEFAULT wrongly_closed,

I have tried to change the quoting to double quotes but still it doesn't work and gives same problem.


Solution

  • I think you need literal here (or you can manually apply the quotes):

    server_default=sa.literal('wrongly_closed')
    

    which generates

    server_default=sa.text("'wrongly_closed'")
    

    resulting in

        Column    │       Type        │ Collation │ Nullable │                Default                
    ══════════════╪═══════════════════╪═══════════╪══════════╪═══════════════════════════════════════
     issue_status │ character varying │           │          │ 'wrongly_closed'::character varying