2

Trying to auto generate initial Alembic files.

I have a flask app and I need to set up Alembic to keep track of the database. I generated a fresh dbmodels.py file with the current schema using

sqlacodegen postgresql://scott:tiger@localhost/mydatabase

I tried running: alembic --autogenerate -m "first" but the generated file is empty:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###

My env.py looks like this:

import os, sys
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(''))), 'App'))
from 
target_metadata = dbmodels.Base.metadata

I tried to examine target_metadata in idle:

from manager import dbmodels

from manager.dbmodels import Base

x = Base.metadata

x

MetaData(bind=None)
SivolcC
  • 3,258
  • 2
  • 14
  • 32
Lev
  • 999
  • 2
  • 10
  • 26
  • Well, if there is no difference between your database and the models definition, alembic will produce an empty migration file. If you want to keep track of changes in your models definition, `alembic init` first, make changes to your models, and only then do a `alembic revision --autogenerate`. – SivolcC Mar 26 '19 at 23:48
  • but doesn't it need to 'record' the tables and columns already in the database? So does alembic only detects the changes in the database and not the models script? – Lev Mar 27 '19 at 01:35
  • 2
    The point of alembic is to apply changes made on a models definition to a database, not the other way around. If I understand well, to answer to your question, alembic does so by creating an `alembic_version` table. If you want to have an alembic script that creates all the tables, run the `generate` against an empty database. – SivolcC Mar 27 '19 at 02:40

0 Answers0