Alembic --autogenerate producing empty migration

杀马特。学长 韩版系。学妹 提交于 2019-11-29 01:24:22

As per @zzzeek, after I included the following in my env.py, I was able to work with --autogenerate option

in env.py under run_migrations_online()

from configuration import app
from core.expense.models import user # added my model here

alembic_config = config.get_section(config.config_ini_section)
alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
engine = engine_from_config(
    alembic_config,
    prefix='sqlalchemy.',
    poolclass=pool.NullPool)

then I ran alembic revision --autogenerate -m "Added initial table" and got

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users',
    sa.Column('uuid', sa.GUID(), nullable=False),
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('password', sa.String(), nullable=False),
    sa.Column('created_on', sa.DateTime(timezone=True), nullable=True),
    sa.Column('last_login', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('email'),
    sa.UniqueConstraint('uuid')
    )
    ### end Alembic commands ###

Thank you Michael for all your help!

I think it's worth pointing out here that I had the same problem in the current version (0.8.4), but the method of setting metadata appears to have become more explicit: In addition to importing a model, you need to also set target_metadata (which is present in env.py but defaults to None).

The documentation suggests importing something they called Base, but it's not clear what exactly that is; importing the DeclarativeBase instance my models inherit from did nothing for me (same result as OP).

The actual comments in the code, though, suggest setting target_metadata using an actual model (ModelNameHere.metadata), which did work for me (using one model's metadata resulted in all of them being detected).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!