Search code examples
pythonpostgresqlsqlalchemymigrationalembic

Alembic autogenerate migration without check contraint


I am new to alembic and sqlalchemy world
Lets say I have model:

class Model(Base):
    __tablename__ = 'models'
    id = Column(Integer, primary_key=True)
    value = Column(Integer, CheckContraint('value >= 0'))

and if I do alembic --config=development.ini revision --autogenerate -m "init" I get for example

def upgrade():
    op.create_table('models',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('value', sa.Integer())

and here I miss create_check_constraint how can I do it automatic or should I add it manually? I'd like it to work with postgresql


Solution

  • Alembic autogenerate does not currently support check constraint detection.

    From https://alembic.sqlalchemy.org/en/latest/autogenerate.html#what-does-autogenerate-detect-and-what-does-it-not-detect :

    Autogenerate can’t currently, but will eventually detect:

    Some free-standing constraint additions and removals, like CHECK, PRIMARY KEY - these are not fully implemented.

    Seems like you'll need to do it manually, e.g. by using execute.