So in Flask I have models.py
which contains all of my model definitions. I'd like to separate this out into multiple model files under a models
directory.
I've given this a try by adding some model files such as models/user_model.py
, models/booking_model.py
etc. but alembic doesn't seem to detect the models in these files.
In the standard alembic.ini with Flask I have:
# A generic, single database configuration.
[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
Do I need to add something here to get alembic to recognise the models?
To complete this answer, you can directly put all your 'Bases' in a list like so :
from myapp.mymodel1 import Model1Base
from myapp.mymodel2 import Model2Base
target_metadata = [Model1Base.metadata, Model2Base.metadata]
Link to the doc : http://alembic.zzzcomputing.com/en/latest/autogenerate.html#autogenerating-multiple-metadata-collections
You need to edit your env.py:
# add your model's MetaData object here
# for 'autogenerate' support
from your_cool_app.models import *
target_metadata = db.Model.metadata
I ran into the exact same thing.
Here's my structure
.. alembic
.. .. alembic stuff
.. database
.. .. __init__.py
.. .. models.py
.. .. my_table_one.py
.. .. my_table_two.py
create base in your init as such:
# creates Base
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
you can also use the init to create an sqlite if it doesnt already exist (I do that)
Then in my models.py I import Base and all my tables
from database import Base
from database.my_table_one import MyTableOne
from database.my_table_two import MyTableTwo
in my tables I import base from the database module
from database import Base
and finally my env.py is very simply this:
from database.models import Base
target_metadata = [Base.metadata]
the reason for this is so that I don't have to maintain env.py every time I add a table. I add it to the models.py in my database module and that's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With