-
-
Save 0bserver07/bd5d7120f399532e805dc2986ab4041c 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