问题描述
我有一个与Flask-migrate相关的问题.我正在用Flask创建一组Web服务.我已经在python应用程序中将每个Web服务拆分为自己的软件包.
应用程序结构如下:
MyApp WS1 models.py WS2 models.py 普通包装 models.py
如何导入所有模块并初始化数据库?我尝试手动将它们全部导入,但无法正常工作.我知道,如果我分别从WS1或Ws2导入"app",它会起作用,但是我想通过一次操作来做到这一点,这可能吗?
在这里您可以找到Flask-migrate的代码:
#!virtualenv/bin/python
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()
这是我初始化SQLAlchemy会话的模块:
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()
所有模型都将导入此模块并从Base继承.
Thanks a lot,
恩里科
在定义自定义声明性基础时,您不再使用Flask-SQLAlchemy
了,并且Flask-Migrate
从Flask-SQLAlchemy
的内部读取模型声明性基础. (这就是为什么必须同时使用app
和db
初始化Migrate
的原因.)
Flask可以解决导入周期问题:大多数Flask扩展都具有.init_app()
方法,可以在需要时推迟初始化扩展.
在当前手动创建Base
的模块中,只需执行db = SQLAlchemy()
,然后在模型中导入db
并使用db.Models
作为声明基础而不是Base
.在您的app.py
中,执行以下操作:
from dbpackagename import db
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db.init_app(app)
I've a question related to Flask-migrate.I'm creating a set of web services with Flask. I've split each web service in his own package in a python application.
The application structure look like this:
MyApp WS1 models.py WS2 models.py CommonPackage models.py
How can I import all the modules and init the db? I've tried to import them all manually but is not working.I know that it works if I import the "app" from WS1 or Ws2 separately, but I would like to do this in a single operation, is this possible?
Here you can find the code for Flask-migrate:
#!virtualenv/bin/python
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()
This is the modules where I initialize 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 the models import this module and inherit from Base.
Thanks a lot,
Enrico
When you are defining a custom declarative base you're not really using Flask-SQLAlchemy
anymore—and Flask-Migrate
reads the models from Flask-SQLAlchemy
's internal declarative base. (That is why you have to initialize Migrate
with both app
and db
).
Flask has an answer to the import cycle problem: Most Flask extensions have an .init_app()
method to defer initializing the extension where necessary.
In the module where you currently create Base
by hand, just do db = SQLAlchemy()
and then in your models import db
and use db.Models
as your declarative base instead of Base
. In your app.py
, do the following:
from dbpackagename import db
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db.init_app(app)
这篇关于烧瓶迁移多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!