Alembic: drop_constraint etc. should check for name=None and emit informative error message since this is common w/ autogenerate

Created on 19 Jul 2019  路  2Comments  路  Source: sqlalchemy/alembic

I made migration with alembic revision --autogenerate which contains addition of foreign key:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('image_resource',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('url', sa.String(length=250), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.add_column('category', sa.Column('image_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'category', 'image_resource', ['image_id'], ['id'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'category', type_='foreignkey')
    op.drop_column('category', 'image_id')
    op.drop_table('image_resource')
    # ### end Alembic commands ###

Upgrade works great, but when I tried to downgrade I got issue:if len(ident) > self.max_identifier_length: TypeError: object of type 'NoneType' has no len() at sqlalchemy/engine/default.py, line 444.

If I manually specify foreign key it works perfectly:

op.create_foreign_key('my_fk', 'category', 'image_resource', ['image_id'], ['id'])

op.drop_constraint('my_fk', 'category', type_='foreignkey')

Is this behaviour is issue?

missing behavior naming convention issues op directives

Most helpful comment

oh and as to why it's coming out as None, you aren't using a naming convention, please see https://alembic.sqlalchemy.org/en/latest/naming.html

All 2 comments

hi there -

it might be nice if alembic had a nicer error message for this case, however, the constraint name cannot be None for drop_constraint(). Otherwise how will the "DROP CONSTRAINT" command know which constraint to drop?

oh and as to why it's coming out as None, you aren't using a naming convention, please see https://alembic.sqlalchemy.org/en/latest/naming.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zpsjs picture zpsjs  路  7Comments

sqlalchemy-bot picture sqlalchemy-bot  路  8Comments

cozos picture cozos  路  5Comments

WilliamMayor picture WilliamMayor  路  3Comments

am17torres picture am17torres  路  7Comments