Alembic --autogenerate Producing Empty Migration
Solution 1:
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!
Solution 2:
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).
Solution 3:
If you don't want your flake8 throwing unused import errors, then you can also add the Record to __all__
in your models file.
At the end of users.py
__all__ = Users
More information at - https://docs.python.org/3/tutorial/modules.html#importing-from-a-package
Solution 4:
Everything was much simplier, just from starting read the doc i solved my issue with auto generating the migrations.
Don't forget to change
target_metadata
from None to Base.metadata
in your env.py file.
target_metadata = Base.metadata
along with the import of the all models you want to track in your __init__.py
of your module that consist all db models that inherits from Base model. Then you just need to import these Base in your env.py
from app.database.modelsimportBase
that's it!
Post a Comment for "Alembic --autogenerate Producing Empty Migration"