在Flask服务器中使用Flask-SQLAlchemy的正确方法是什么? [英] What is correct way to use Flask-SQLAlchemy in Flask server?

查看:81
本文介绍了在Flask服务器中使用Flask-SQLAlchemy的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模型代码,我已经测试过该代码,没有错误,它可以在DB中创建表,记录

This is model code, I have tested this code, it is no error and it can create tables, records in DB

createdb.py

createdb.py

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@localhost:3306/ai'
db = SQLAlchemy(app)

if __name__ == 'createdb':
    db.reflect()
    db.drop_all()
    db = SQLAlchemy(app)

class Class(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True)
    label = db.Column(db.String(255), unique=True, nullable=False)

    def __init__(self, label):
        self.label = label

    def __repr__(self):
        return '<Class %r>' % self.username

class Photo(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True)
    path = db.Column(db.String(1024), nullable=False)

    def __init__(self, path):
        self.path = path

    def __repr__(self):
        return '<Photo %r>' % self.username

class PhotoClass(db.Model):
    id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True)
    photoId = db.Column(db.Integer, db.ForeignKey('photo.id'), nullable=False)
    classId = db.Column(db.Integer, db.ForeignKey('class.id'), nullable=False)
    score = db.Column(db.Float, nullable=False)

    def __init__(self, photoId, classId):
        self.photoId = photoId
        self.classId = classId

    def __repr__(self):
        return '<PhotoClass %r>' % self.username

if __name__ == 'createdb':
    db.create_all()
    db.session.add(Class('Plain'))
    db.session.add(Class('Printed'))
    db.session.commit()

这是服务器代码

app.py

import createdb

app = Flask(__name__)

@app.route('/')
def index():
    createdb.db.session.add(createdb.Class('aaa'))
    createdb.db.session.commit()
    return render_template('index.html')

if __name__ == '__main__':
    app.run()

如果我导入到服务器,它仍然没有错误,当我进入localhost:5000时,会出现此错误

If I import to server, it is still no error, when I go to localhost:5000, I will get this error

track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'


这是完全错误


This is full error

[2018-10-30 18:31:03,288] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "C:\python\lib\site-packages\sqlalchemy\util\_collections.py", line 999, in __call__
    return self.registry[key]
KeyError: 12344

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\python\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\python\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\python\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\python\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\python\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\python\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "E:\0\airbtn\yeetungaiserver\app.py", line 17, in index
    createdb.db.session.add(createdb.Class('aaa'))
  File "C:\python\lib\site-packages\sqlalchemy\orm\scoping.py", line 153, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "C:\python\lib\site-packages\sqlalchemy\util\_collections.py", line 1001, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "C:\python\lib\site-packages\sqlalchemy\orm\session.py", line 2950, in __call__
    return self.class_(**local_kw)
  File "C:\python\lib\site-packages\flask_sqlalchemy\__init__.py", line 142, in __init__
    track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

推荐答案

createdb.py 中:

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@localhost:3306/ai'
db = SQLAlchemy(app)

if __name__ == 'createdb':
    db.reflect()
    db.drop_all()
    db = SQLAlchemy(app)

您创建一个名为 app Flask 实例,并将其传递给 SQLAlchemy 构造函数,将结果分配给变量 db(实际上是从 app.py 导入 createdb 时执行两次).他们的关键是引用 Flask 实例 createdb.app 实例化 db .

you create an instance of Flask called app and pass that to the SQLAlchemy constructor assigning the result to the variable, db (you actually do this twice when you import createdb from app.py). They key point is that db is instantiated referencing the Flask instance createdb.app.

然后在app.py中输入

Then in app.py:

import createdb  # createdb.app is created upon import

app = Flask(__name__)  # but you create a new `Flask` instance called app

@app.route('/')
def index():
    # all of the below db operations occur on createdb.db which is in the 
    # createdb.app context, not the context of app created in this module
    createdb.db.session.add(createdb.Class('aaa'))
    createdb.db.session.commit()
    return render_template('index.html')

if __name__ == '__main__':
    # the app you run here, isn't the app that was made available to db in 
    # in createdb.py. It has no idea what db is!
    app.run()
    # try createdb.app.run()

我在上面详细注释了您的app.py代码,但简短之处在于 app.py 中的 app.run()调用不是't在 createdb.db 知道的 app 上调用.

I've annotated your app.py code above with more detail but the short of it is that the app.run() call in app.py isn't called on the app that createdb.db is aware of.

这篇关于在Flask服务器中使用Flask-SQLAlchemy的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆