Flask-Sqlalchemy + Sqlalchemy-searchable returning empty list

后端 未结 2 1330
无人及你
无人及你 2021-02-06 04:22

First time on the site, so hi to all and thanks in advance. Longtime lurker and newb.

I\'m working on a web app in flask, using Flask-SqlAlchemy and SqlAlchemy-Searchabl

相关标签:
2条回答
  • 2021-02-06 04:38

    On Collin Allen's answer: actually, the flask-sqlalchemy ''db'' exposes the configure_mappers function.

    Replace:

    from sqlalchemy.orm.mapper import configure_mappers
    ...
    configure_mappers()
    

    with:

    ...
    db.configure_mappers()
    
    0 讨论(0)
  • 2021-02-06 04:41

    I ran into this exact issue once, too, when using Flask-Script to add a manage.py management tool to my application.

    The fact that the search_vector column is empty despite you having added the appropriate TSVectorType parameters means that the SQLAlchemy-Searchable trigger isn't present in the postgres DB. You can verify its absence by doing a \df+ in psql command line tool -- you will not see a trigger named article_search_vector_update. SQLAlchemy-Searchable sets up this trigger to update the content of the search_vector column when the columns named in TSVectorType(...) change.

    In the case of manage.py, I had to first call:

    db.configure_mappers()
    

    Essentially, you have to configure SQLAlchemy's mappers before calling create_all(). Without doing this, SQLAlchemy-Searchable will not be given the opportunity to add its search_vector trigger to populate the TSVectorType column in the model.The SQLAlchemy-Searchable docs have more on this.

    In total, a minimal manage.py that properly configures SQLAlchemy-Searchable as you require might look like:

    #!/usr/bin/env python
    
    from flask.ext.script import Manager
    from app import app, db
    
    manager = Manager(app)
    
    @manager.command
    def init_db():
        """
        Drops and re-creates the SQL schema
        """
        db.drop_all()
        db.configure_mappers()
        db.create_all()
        db.session.commit()
    
    0 讨论(0)
提交回复
热议问题