Skip to content Skip to sidebar Skip to footer

Prevent Alembic From Autogenerating Tables

I'm new to so alembic so i might miss a point in its concept but here is the question. i have some sqlalchemy tables in a flask app like this: class Data(Base): __tablename__ = 'Da

Solution 1:

Your migration explicitly create a Data table:

defupgrade():
    ...
    op.create_table('Data',
    ...

So if your Data table already exists because you already created it manually, it is normal to get an error.

EDIT: I am not sure to understand when it is executed but you may want to try commenting the Base.metadata.create_all(engine) line in your database initialization script. I suspect it to create the tables. I have never seen alembic create tables before running the migrations (this is the job of migrations to create tables), if it does not solve your problem I think the problem is not from alembic.


Alembic is intended to manage your database migration from the start, it does not assume that you already created your tables.

Basically it creates a table to keep history of migrations applied to the database. When you run the first upgrade, there is no applied migration yet, so Alembic will try to run all the migration upgrades from the root one (whose down_revision is None) to the head one. At each applied migration, it also updates its history table to reflect the database state.

You could (ordered by my preference level):

  1. drop your already existing tables and let alembic create them. This way, Alembic just create the table as declared in the migration and updates its history.

  2. make Alembic believe that it already applied the first migration by filling manually its history table (I have never done that but I think it is possible). This way it will not try to apply it again

  3. remove the create_table directives from your root migration's upgrade() function (and probably the drop_table from the downgrade() function). This way, Alembic will run the migration without trying to create the already existing tables and it should work. It will also record the migration as applied in its own history.

  4. add a test in your migration to create the table only if it does not already exist, but in this case how will you manage the downgrade?

Post a Comment for "Prevent Alembic From Autogenerating Tables"