0

I'm setting up alembic for our project, which is already really big, and has a lot of tables. The thing is that our project's DB has been managed via SQL for a long time, and our Alchemy models are almost all reflected like so (I obscured the name, but the rest is all directly from our code):

class SomeModel(Base, BaseModelMixin):
    """
    Model docstring
    """

    """Reflect Table"""
    __table__ = Table('some_models', metadata, autoload=True)

What's happening is that when I create an automatic migration, a lot of drop table (and a lot of create table) operations are created for some reason. I assumed it's because the model class doesn't explicitly define the tables, but I don't see why that would drop the tables as well.

I'm making sure all model definitions are processed before setting the target_metadata variable in env.py:

# this imports every model in our app

from shiphero_app.utils.sql_dependencies import import_dependencies
import_dependencies()

from shiphero_app.core.database import Base
target_metadata = Base.metadata

Any ideas of what might I be missing here?

Lacrymology
  • 2,269
  • 2
  • 20
  • 28

1 Answers1

0

This is probably what you are looking for - this makes Alembic ignore predefined tables: https://alembic.sqlalchemy.org/en/latest/cookbook.html#don-t-generate-any-drop-table-directives-with-autogenerate

Unfortunately this also prevents Alembic from dropping tables within scope as well

user787267
  • 2,550
  • 1
  • 23
  • 32