所以在Flask中我有models.py
,其中包含我的所有模型定义。我想将其分成models
目录下的多个模型文件。
我尝试添加一些模型文件,例如models/user_model.py
,models/booking_model.py
等,但是alembic似乎没有检测到这些文件中的模型。
在带有Flask的标准alembic.ini中我有:
# 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

我是否需要在此处添加一些内容以使alembic能够识别模型?
答案 0 :(得分:3)
您需要编辑env.py:
# add your model's MetaData object here
# for 'autogenerate' support
from your_cool_app.models import *
target_metadata = db.Model.metadata
答案 1 :(得分:3)
要完成此答案,您可以将所有“基本信息”直接放入列表中,如下所示:
from myapp.mymodel1 import Model1Base
from myapp.mymodel2 import Model2Base
target_metadata = [Model1Base.metadata, Model2Base.metadata]