简体   繁体   English

使用 whoosh_search 和分页时出现 sqlalchemy 错误

[英]sqlalchemy error when using whoosh_search and pagination

I am trying to paginate a text search result using sqlalchemy (I use flask-sqlalchemy) and whoosh.我正在尝试使用 sqlalchemy(我使用flask-sqlalchemy)和 whoosh 对文本搜索结果进行分页。 My simplified models.py looks like this我简化的 models.py 看起来像这样

class User(db.Model):
    __searchable__ = ['username']

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)

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

whooshalchemy.whoosh_index(app, User)

So I simply want to search my database for usernames.所以我只想在我的数据库中搜索用户名。 I thought I can do that with我以为我可以做到这一点

search_result = models.User.query.whoosh_search(search_txt, MAX_SEARCH_RESULTS).paginate(1, MEMBERS_PER_PAGE, False)

where search_txt is my search text.其中 search_txt 是我的搜索文本。 This works if the result of the search is only one user.如果搜索结果只有一个用户,则此方法有效。 But if it is more than one user I get the following error但是如果有多个用户,我会收到以下错误

AttributeError: 'result' object has no attribute 'id' AttributeError: 'result' 对象没有属性 'id'

I just started with sqlalchemy so it is probably some very simple mistake on my side?我刚开始使用 sqlalchemy,所以这可能是我这边的一些非常简单的错误? thanks for your help carl谢谢你的帮助卡尔

edit: Ok figured out that the problem is connected to the variable MEMBERS_PER_PAGE.编辑:确定问题与变量 MEMBERS_PER_PAGE 有关。 if MEMBERS_PER_PAGE is equal the number of search results, I get an error.如果 MEMBERS_PER_PAGE 等于搜索结果的数量,我会收到错误消息。 Otherwise it works fine...否则它工作正常......

This seems to be a bug in Flask-WhooshAlchemy.这似乎是 Flask-WhooshAlchemy 中的一个错误。 There is a open pull request fixing the problem on the github page of Flask-WhooshAlchemy.在 Flask-WhooshAlchemy 的github页面上有一个解决问题的开放拉取请求。 I ended up using Flask-WhooshAlchemy-Redux , as suggested in the discussion on the pull request, and that fixed the pagination problem for me.我最终使用了Flask-WhooshAlchemy-Redux ,正如关于拉取请求的讨论中所建议的那样,这为我解决了分页问题。

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

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