2

If I have an init.py file which looks as such:

from sqlalchemy import create_engine
import os
from sqlalchemy.orm import sessionmaker, scoped_session
from testserver.database.models.Base import Base
from sqlalchemy.ext.declarative import declarative_base
from flask_migrate import Migrate

BASE_DIR = os.path.dirname(os.path.realpath(__file__))


def set_db_connection():
    connection_string = 'sqlite:///' + os.path.join(BASE_DIR, 'testserver.db')
    return connection_string


engine = create_engine(set_db_connection(), connect_args={'check_same_thread': False})
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Set the custom base class
Base = declarative_base(cls=Base)


def init_app(app):
    Base.metadata.create_all(engine)
    Base.metadata.bind = engine
    Session.configure(bind=engine)
    app.session = scoped_session(Session)



# Export session for use in other classes
db_session = Session()

If I am to make an update to my existing models. Can I use the flask_migrate package to successfully run a migration? And if so how?

Ben_Sven_Ten
  • 529
  • 3
  • 21

1 Answers1

3

Author of Flask-Migrate here. Flask-Migrate needs Flask-SQLAlchemy or Alchemical (a still somewhat experimental package I built that uses the newer query functionality in SQLAlchemy 1.4).

If you use plain SQLAlchemy, then work with Alembic directly.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152