如何修复AttributeError:'Mapper'对象没有属性'persist_selectable'

时间:2019-10-03 06:55:56

标签: python python-3.x postgresql sqlalchemy flask-sqlalchemy

我刚刚使用过,出现错误,请帮助我解决

不使用sqlalchemy_searchable一切正常

import os
from datetime import datetime
from flask import Flask
from sqlalchemy.orm.mapper import configure_mappers
from flask_sqlalchemy import SQLAlchemy, BaseQuery
from sqlalchemy_searchable import SearchQueryMixin
from sqlalchemy_utils.types import TSVectorType
from sqlalchemy_searchable import make_searchable

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = 'samplesecret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost:5432/blog'

db = SQLAlchemy()
make_searchable(db.metadata)

class PostQuery(BaseQuery, SearchQueryMixin):
    pass

class Post(db.Model):
    query_class = PostQuery
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    search_vector = db.Column(TSVectorType('title', 'content'))
    image_thumb = db.Column(db.String(255), nullable=False, default='default.jpg')
    slug = db.Column(db.Text)
    publish = db.Column(db.Boolean(), nullable=False, server_default='1')
    headline = db.Column(db.Boolean(), nullable=False)
    dibaca = db.Column(db.Integer, nullable=False, default=0)

    def __repr__(self):
        return "Post('{self.title}', '{self.date_posted}')"

db.configure_mappers() #very important!

print(Post.query.search(u'something').limit(5).all())

当我运行错误消息AttributeError时:'Mapper'对象没有属性'persist_selectable', 如何解决该错误,我尝试了不支持python3的whoosaqlalchemy,因为我使用sqlalchemy_searchable

此完整的追溯

Traceback (most recent call last):
  File "run.py", line 23, in <module>
    class Post(db.Model):
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 609, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\ext\declarative\api.py", line 65, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 116, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 144, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 176, in __init__
    self._early_mapping()
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 179, in _early_mapping
    self.map()
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 623, in map
    **self.mapper_args
  File "<string>", line 2, in mapper
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 694, in __init__
    self._configure_class_instrumentation()
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 1230, in _configure_class_instrumentation
    self.dispatch.instrument_class(self, self.class_)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\event\attr.py", line 246, in __call__
    fn(*args, **kw)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\orm\events.py", line 621, in wrap
    fn(*arg, **kw)
  File "C:\Users\dhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy_searchable\__init__.py", line 291, in process_mapper
    columns = self.inspect_columns(mapper.persist_selectable)
AttributeError: 'Mapper' object has no attribute 'persist_selectable'

0 个答案:

没有答案