简体   繁体   English

对多个文件中的模型使用 Flask-migrate

[英]Using Flask-migrate for models in multiple files

I am confused about how to use Flask-Migrate when I have multiple models.当我有多个模型时,我对如何使用 Flask-Migrate 感到困惑。 Basically my Flask app looks like this:基本上我的 Flask 应用程序如下所示:

app
├── __init__.py
├── config.py
├── manage.py
├── migrations
├── models
│   ├── model1.py
│   ├── model2.py
├── resources
├── run.py
└── tests

I've read that for each model its best to create the db = SQLAlchemy() object in the file and then import this db object into the app's __init__.py like so:我已经阅读过,对于每个模型,最好在文件中创建db = SQLAlchemy()对象,然后将此 db 对象导入应用程序的__init__.py中,如下所示:

from models.model1 import db
db.init_app(app)
from models.model2 import db
db.init_app(app)

However if I do this for multiple model files, how can I add Flasks's migrate functionality, considering I can only use 1 sql alchemy object for the migrate class instantiation:但是,如果我对多个模型文件执行此操作,考虑到我只能使用 1 个 sql alchemy 对象进行迁移类实例化,我该如何添加 Flasks 的迁移功能:

migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)

Would it be best in this case to define a single sql alchemy object in the __init__.py file and import that into all my models?在这种情况下,最好在__init__.py文件中定义一个 sql alchemy 对象并将其导入我的所有模型中吗?

You misread the referenced text.您误读了引用的文本。 That talks about something completely different.那是在谈论完全不同的事情。 That talks about keeping your db object separate from the app object (and tie the two togther in the create_app factory function).这谈到了将您的db对象与app对象分开(并在create_app工厂函数中将两者联系在一起)。 Creating multiple db objects is only complicating matters for you.创建多个db对象只会让您的事情变得复杂。

All that is needed is a single db = SQLAlchemy() object, and all the files that define models need to be imported.只需要一个db = SQLAlchemy()对象,所有定义模型的文件都需要导入。 Usually that's done directly or indirectly via your create_app factory function, You need to call the create_app() function anyway to be able to run the flask db command-line tool anyway .通常这是通过您的create_app工厂函数直接或间接完成的,您无论如何都需要调用create_app()函数才能运行flask db命令行工具。

Next, you do not need to create a manager either .接下来,您也不需要创建管理器。 The Manager object is a hold-over from the time before the Flask project added support for scripts itself. Manager对象是 Flask 项目添加对脚本本身的支持之前的保留。 If you are using Flask 0.12 or newer, you don't want to be using Flask-Script and it's manager.如果您使用的是 Flask 0.12 或更新版本,您不想使用 Flask-Script 和它的管理器。

So, all you need, in your __init_.py , is:因此,在您的__init_.py中,您只需要:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate


db = SQLAlchemy()


def create_app(test_config=None):
    app = Flask(__name__)

    app.config.from_object(f"{__name__}.config")
    app.config.from_envvar("PROJECTNAME_SETTINGS", silent=True)
    if test_config:
        app.config.from_mapping(test_config)

    db.init_app(app)
    Migrate(app, db)

    # importing the models to make sure they are known to Flask-Migrate
    from models import models1, models2

    # any other registrations; blueprints, template utilities, commands

    return app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM