Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get alembic to recognise models from multiple model files in Flask

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?

like image 237
John Mike Avatar asked Aug 28 '17 13:08

John Mike


3 Answers

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

like image 190
Thaume Avatar answered Oct 08 '22 12:10

Thaume


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
like image 9
Aleksandr Borisov Avatar answered Oct 08 '22 12:10

Aleksandr Borisov


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.

like image 1
David Mendes Avatar answered Oct 08 '22 14:10

David Mendes