Created
August 8, 2016 01:50
-
-
Save logston/e0ab3e46dd51b27f83b5d8ad359289ad to your computer and use it in GitHub Desktop.
A snippet for getting Alembic to recognize multiple model classes in multiple modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired by http://liuhongjiang.github.io/hexotech/2015/10/14/alembic-support-multiple-model-files/ | |
def combine_metadata(): | |
from sqlalchemy import MetaData | |
import models # models file into which all models are imported | |
model_classes = [] | |
for model_name in models.__all__: | |
model_classes.append(getattr(models, model_name)) | |
m = MetaData() | |
for model in model_classes: | |
for t in model.metadata.tables.values(): | |
t.tometadata(m) | |
return m | |
target_metadata = combine_metadata() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since Alembic 0.9.0 we can simply do this:
target_metadata = [Model1Base.metadata, Model2Base.metadata]
.