1. Michael Bayer
  2. alembic
  3. Issues


Issue #161 new

Avoid describing twice in migration scripts

created an issue


I'm pretty new with alembic, but I use it for the migration of my production database at work.

I often need to migrate data, so I have to hack a bit the auto generated script. However, I need to describe my change twice:

  • first, I reuse the operation stuff generated by the autogenerate option does its job:
op.create_table('the_new_table', sa.Column(...))
  • second, I need to fill this table. Since I need SQLAlchemy object, I need to redescribe it again:
   the_new_table = sa.Table('the_new_table',

Note: I needed to use sa.Table in order to have the autoincrement working, because I didn't wanted to work with a sa.sql.table object...

and then I can use execute/select/insert/... on the the_new_table.c object.

I have two solutions to propose: 1. let op.create_table,... accept sa.sql.table or sa.Table objects 2. return a sa.sql.table (or sa.Table) object after create_table.

Or if neither solution is correct, can you enhance the documentation (and the tutorial) to fulfill this use case?

This should be the same for create_column.

Many thanks. Gaetan

Comments (5)

  1. Michael Bayer repo owner

    here's what you do:

    starting with:

    op.create_table('the_new_table', sa.Column(...))

    change it to:

    t = Table('the_new_table', metadata, sa.Column(...))

    then call:


    get_bind is here: http://alembic.readthedocs.org/en/latest/ops.html#alembic.operations.Operations.get_bind create() is here: http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html#creating-and-dropping-database-tables

    if you want specific docs in alembic's API docs please feel free to submit a pull request for consideration.

  2. Log in to comment