本文介绍了烧瓶迁移多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与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-MigrateFlask-SQLAlchemy的内部读取模型声明性基础. (这就是为什么必须同时使用appdb初始化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)

这篇关于烧瓶迁移多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:53