Skip to content

CREATE INDEX (possibly other constraints, need to test) missing when table is dropped in the upgrade #468

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by Michael Bayer (@zzzeek)

Step 1, env.py:

from sqlalchemy import *

target_metadata = m = MetaData()

Table(
    'a', m, Column('id', Integer, primary_key=True),
    Column('q', Integer, index=True)
)

step 2:

#!

[classic@photon2 alembic]$ PYTHONPATH=~/dev/sqlalchemy/lib/ python -m alembic.config revision -m "rev1" --autogenerate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'a'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_a_q' on '['q']'
  Generating /home/classic/dev/alembic/foo/versions/54630d783fa6_rev1.py ... done
[classic@photon2 alembic]$ PYTHONPATH=~/dev/sqlalchemy/lib/ python -m alembic.config upgrade head
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 54630d783fa6, rev1

migration file has:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('a',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('q', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_a_q'), 'a', ['q'], unique=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_a_q'), table_name='a')
    op.drop_table('a')
    # ### end Alembic commands ###

now remove the table:

from sqlalchemy import *

target_metadata = m = MetaData()

#Table(
#    'a', m, Column('id', Integer, primary_key=True),
#   Column('q', Integer, index=True)
#)

run again:

[classic@photon2 alembic]$ PYTHONPATH=~/dev/sqlalchemy/lib/ python -m alembic.config revision -m "rev2" --autogenerate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected removed table u'a'
  Generating /home/classic/dev/alembic/foo/versions/d8d7ee4f2f3b_rev2.py ... done

migration file is missing the index:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('a')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('a',
    sa.Column('id', mysql.INTEGER(display_width=11), nullable=False),
    sa.Column('q', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_default_charset=u'utf8',
    mysql_engine=u'InnoDB'
    )
    # ### end Alembic commands ###

we need to generate MigrateTableOps for tables that are removed during autogenerate.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions