根据this guide,下面是我的models.py文件中的代码,它们位于最底部
db.configure_mappers()
db.create_all()
db.commit()
这将在导入时执行。因此,现在,每当我导入模型时,我都会收到以下消息:
RuntimeError: application not registered on db instance and no applicationbound to current context
我不确定如何将这些代码融入到工厂范例中。我尝试将代码包装在一个函数中,然后在create_app
中调用它。
当我这样做的时候,我得到的错误是:
sqlalchemy.exc.CompileError: (in table 'ad', column 'search_vector'): Compiler <sqlalchemy.dialects.sqlite.base.SQLiteTypeCompiler object at 0x7f81bfc14940> can't render element of type <class 'sqlalchemy.dialects.postgresql.base.TSVECTOR'>
search_vector专栏非常简单,取自sqlalchemy-searchable快速入门指南:
search_vector = db.Column(TSVectorType('title', 'body'))
发布于 2017-09-27 15:37:03
你的方法是在函数中正确包装初始化代码:
from models import db
def create_app():
app = Flask(__name__)
db.init_app(app)
db.configure_mappers()
db.create_all()
db.commit()
return app
app = create_app()
或者,如果你想避免弄乱你的启动文件,定义一个以应用程序为参数的函数(models.py):
def init_db(app):
db.init_app(app)
db.configure_mappers()
db.create_all()
db.commit()
并使用该函数初始化应用程序:
from models import init_db
app = Flask(__name__)
init_db(app)
但是你的错误是由另一个问题引起的: SQLite不支持TSVector,它是PostgreSQL特有的特性。SQAlchemy-Searchable的所有示例都是为PostgreSQL数据库编写的。
看起来SQLite也没有开箱即用的全文搜索功能。也许您需要切换到另一个DBMS。
https://stackoverflow.com/questions/46413062
复制相似问题