I have a question related to Flask-migrate. I am creating a web services suite with Flask. I have divided each web service in my package in a python application.
The structure of the application is as follows:
MyApp WS1 models.py WS2 models.py CommonPackage models.py
How can I import all modules and initialize db? I tried to import them all manually, but it doesn’t work. I know that it works if I import the “application” from WS1 or Ws2 separately, but I would like to do it in one operation, is this possible?
Here you can find the code for Flask-migrate:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from config import SQLALCHEMY_DATABASE_URI
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
from WS1.models import Class1, Class2, Class3 <--- This is not importing
from WS2.models import Class4, Class5, Class6 <--- This is not importing
if __name__=='__main__':
manager.run()
These are the modules in which I initialize the SQLAlchemy session:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from config import SQLALCHEMY_DATABASE_URI
engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode = True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
All models import this module and inherit from Base.
Thanks a lot,
Enrico