[英]flask-sqlalchemy timeouterror by queuepool overflow using backref option
i'm beginner with flask-sqlalchemy.我是 flask-sqlalchemy 的初学者。 i encountered a problem maybe caused by using backref.我遇到了一个问题,可能是由于使用 backref 引起的。
the api(view api. it renders template) is like this. api(查看 api。它呈现模板)是这样的。
@board.route('/')
def index():
page = request.args.get('page', 1, type=int)
q = Article.query.order_by(Article.create_date.desc())
article_list_pagination = q.paginate(page, per_page=10)
return render_template('board.html', article_list=article_list_pagination)
and the model is like this model 是这样的
class Answer(db.Model):
__tablename__ = 'answer'
id = db.Column(db.Integer, primary_key=True)
article_id = db.Column(db.Integer, db.ForeignKey('article.id', ondelete='CASCADE'))
article = db.relationship('Article', backref=db.backref('answer_set'))
user_id = db.Column(db.String(255), nullable=False)
name = db.Column(db.String(255), nullable=False)
content = db.Column(db.Text(), nullable=False)
create_date = db.Column(db.DateTime(), nullable=False)
update_date = db.Column(db.DateTime(), nullable=True)
when i request the api every 20 times, i get this error当我每 20 次请求 api 时,我收到此错误
sqlalchemy.exc.TimeoutError: QueuePool limit of size 10 overflow 10 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/14/3o7r)
here is what i tried to solve the error.这是我试图解决错误的方法。
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Article at 0x1772756baf0> is not bound to a Session; lazy load operation of attribute 'answer_set' cannot proceed
how can i get over this problem?我怎样才能克服这个问题?
+++ +++
when I remove article = db.relationship('Article', backref=db.backref('answer_set'))
from the model and add db.session.close()
before return template, it doesn't make error, but i need it.当我从 model 中删除article = db.relationship('Article', backref=db.backref('answer_set'))
并在返回模板之前添加db.session.close()
时,它不会出错,但我需要它。
what should i do?我应该怎么办?
+++++ answers for comment @branco +++++ 评论的答案@branco
i'm just using flask-sqlalchemy paginate method.我只是使用flask-sqlalchemy 分页方法。 https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/ https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/
and i'm running this app at app.py我在 app.py 运行这个应用程序
from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
import config
db = SQLAlchemy()
migrate = Migrate()
def create_app():
app = Flask(__name__)
app.secret_key = 'secret_key_for_flash'
app.config.from_object(config)
db.init_app(app)
migrate.init_app(app, db)
from models.user import User
from models.article import Article
from models.answer import Answer
from blueprints.main import main
from blueprints.login import login
from blueprints.register import register
from blueprints.board.board import board
from blueprints.board.article import article
from blueprints.board.answer import answer
app.register_blueprint(main)
app.register_blueprint(login)
app.register_blueprint(register)
app.register_blueprint(board)
app.register_blueprint(article)
app.register_blueprint(answer)
app.config['JWT_SECRET_KEY'] = 'jwt_secret_key'
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = config.expires_access
app.config['JWT_REFRESH_TOKEN_EXPIRES'] = config.expires_refresh
app.config['JWT_TOKEN_LOCATION'] = ['cookies']
JWTManager(app)
return app
if __name__ == '__main__' :
create_app().run(debug=True)
finally, i solved this problem.最后,我解决了这个问题。 i just gave lazy options to db.relationship.我只是给 db.relationship 提供了惰性选项。 but didn't work.但没有用。 i had to give lazy option to db.backref.我不得不给 db.backref 提供惰性选项。 like喜欢
article = db.relationship('Article', backref=db.backref('answer_set', lazy='joined'))
and added并添加
db.session.close()
after i called paginate method在我调用分页方法之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.