【问题标题】:SQLAlchemy RecursionError: maximum recursion depth exceeded while calling a Python objectSQLAlchemy RecursionError:调用 Python 对象时超出最大递归深度
【发布时间】:2022-04-03 01:02:43
【问题描述】:

我目前很难理解为什么我会使用下面的代码从标题中得到递归错误。仅当在调用 results = results.all() 之前命中任何一个 for 循环(在 if ops 和 if 合规性下)时才会出现此错误。

@main.route('/search_results', methods=['GET', 'POST'])
@login_required
def search_results():
    """ Here we make the search request and then display the results.  We bring in
    the params via the url, then use whooshalchemy to search fields we marked
    with __searchable__ in the model.  If advanced search is selected, we then filter
    the results before we display.  Note that there are two separate searches for the
    separate data types. """
    subject = request.args.get('subject')
    search_type = request.args.get('search_type')
    acct_no = request.args.get('acct_no')
    id_ = request.args.get('id_')
    rep = request.args.get('rep')
    ops = request.args.get('ops')
    compliance = request.args.get('compliance')
    results = []

    ...

    else:
        results = db.session.query(Envelope)
        if subject is not None and subject is not '':
            results = results.filter(Envelope.subject.like('%'+subject+'%'))
        if acct_no is not None and acct_no is not '':
            results = results.filter_by(acct_no=acct_no)
        if id_ is not None and id_ is not None:
            id_ = int(id_)
            results = results.filter_by(envelope_id=id_)
        if rep is not None and rep is not '' :
            results = results.filter_by(initiator=rep)
        if ops is not None and ops is not '':
            ops_name = external_db.get_fullname_from_username(ops)
            for result in results:
                if db.session.query(Envelope_recipient).filter_by(envelope_id=result.envelope_id,role='Operations',name=ops_name).first() == None:
                    results = results.filter(Envelope.envelope_id != result.envelope_id)
        if compliance is not None and compliance is not '':
            compliance_name = external_db.get_fullname_from_username(ops)
            for result in results:
                if db.session.query(Envelope_recipient).filter_by(envelope_id=result.envelope_id,role='Compliance',name=compliance_name).first() == None:
                    results = results.filter(Envelope.envelope_id != result.envelope_id)

    #results.all() is a list of all esignature.models.Envelope or .Process_item objects
    results = results.all()

    return render_template('search_results.html', subject=subject,
                        search_type=search_type, acct_no=acct_no,
                        id_=id_, rep=rep, ops=ops,
                        compliance=compliance, results=results)

奇怪的是,出于某种原因,代码只能使用一个名称,而不能使用其他名称。如果您需要任何其他信息,我很乐意提供,谢谢!

【问题讨论】:

  • 您似乎在循环results 并在循环内修改results。我不确定这是否会导致您遇到错误,但它不是很有效。更好的方法可能是循环 results 并收集要在列表中过滤掉的 id,然后执行 results = results.filter(not_(Envelope.envelope_id.in_(list_of_unwanted_ids))) 之类的操作(您可能必须从 sqlalchemy 导入 not_)。
  • 居然解决了,非常感谢!

标签: python recursion sqlalchemy flask-sqlalchemy


【解决方案1】:

您似乎在循环遍历结果并修改循环内的结果。我不确定这是否会导致您遇到错误,但它不是很有效。更好的方法可能是循环遍历结果并收集要在列表中过滤掉的 id,然后执行类似

from sqlalchemy import not_
...
results = db.session.query(Envelope).filter(not_(Envelope.envelope_id.in_(list_of_unwanted_ids)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 2020-01-05
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多