如何将flask-sqlalchemy 与现有的sqlalchemy 模型一起使用? [英] How to use flask-sqlalchemy with existing sqlalchemy model?
问题描述
我已经阅读了 flask-sqlalchemy 或 sqlalchemy,其中推荐使用flask-sqlalchemy烧瓶.我想遵循这种方法.
I've read flask-sqlalchemy or sqlalchemy which recommends use of flask-sqlalchemy with flask. I want to follow this approach.
但是,我有一个为命令行脚本编写的现有模型,它基于 sqlalchemy 的 declarative_base,例如,
However, I have an existing model written for command line scripts which is based on sqlalchemy's declarative_base, e.g.,
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base() # create sqlalchemy Base class
:
class Runner(Base):
etc.
我希望仍然能够在此模型中使用命令行脚本,但也希望围绕该模型构建一个 Web 应用程序.
I want to still be able to use the command line scripts with this model, but also want to build a web application around the model.
有没有办法扩展现有模型,以获得使用flask-sqlalchemy 扩展的好处?或者我应该自己动手,并使用 sqlalchemy 的 ScopedSession?
Is there a way to extend the existing model, to gain the benefit of using the flask-sqlalchemy extension? Or should I just roll my own, and use sqlalchemy's ScopedSession?
推荐答案
目前,这不是很受支持,但并非不可能做到.请参阅 Flask-SQLAlchemy 问题列表上的 this issue,其中承认当前的实现扩展使这种情况比他们想象的更令人头疼.希望将来能得到更好的支持(一旦确定了可靠的迁移路径和新的 API).
Currently, this is something that is not well supported, but not impossible to do. See this issue on the Flask-SQLAlchemy issue list, which admits that the current implementation of the extension makes this situation more of a headache than they think it should. Hopefully this will be better supported in the future (once a solid migration path and new API is determined).
该问题给出了以下代码示例:
That issue gives the following code sample:
from flask import Flask
from models import Base, User # Your non-Flask-SQLAlchemy models...
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
@app.before_first_request
def setup():
# Recreate database each time for demo
Base.metadata.drop_all(bind=db.engine)
Base.metadata.create_all(bind=db.engine)
db.session.add(User('Bob Jones', 'bob@gmail.com'))
db.session.add(User('Joe Quimby', 'eat@joes.com'))
db.session.commit()
@app.route('/')
def root():
users = db.session.query(User).all()
return u"<br>".join([u"{0}: {1}".format(user.name, user.email) for user in users])
if __name__ == '__main__':
app.run('127.0.0.1', 5000)
这里有几点需要注意:
首先,您失去了执行 User.query
的能力(因为 User 是使用自己的声明性基础创建的),以及 Flask-SQLAlchemy 的 db.Model 为您提供(例如自动生成表名和方法的能力,如 first_or_404()
).
First, you lose the ability to do User.query
(because User was created using its own declarative base), along with all of the other stuff that Flask-SQLAlchemy's db.Model gives you (such as the ability to auto-generate the table names and methods like first_or_404()
).
其次,任何时候需要做涉及元数据的事情(比如drop_all 或 create_all),您不能使用 Flask-SQLAlchemy 方法.您必须使用绑定到 Flask-SQLAlchemy 引擎的原始元数据.
Second, any time you need to do things that involve the metadata (such as drop_all or create_all), you cannot use the Flask-SQLAlchemy methods. You must use the original metadata, bound to the Flask-SQLAlchemy engine.
我自己没有尝试过,所以我不确定这种方法是否还有其他问题.如果您找到任何门票,您可能想参与其中.
I haven't tried this myself, so I'm not sure if there are any other gotchas to this approach. You might want to participate in that ticket if you find any.
这篇关于如何将flask-sqlalchemy 与现有的sqlalchemy 模型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!