SQLAlchemy给出错误异常:没有为模型Post定义可搜索的列

时间:2013-08-16 17:49:49

标签: python orm sqlalchemy flask-sqlalchemy

这是我的models.py

from app import db

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_searchable import Searchable
from sqlalchemy_utils.types import TSVectorType

Base = declarative_base()

tags = db.Table('tags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
    db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)

class Post(db.Model, Base, Searchable):
    __tablename__ = 'post'
    __searchable_columns = ['title', 'body']
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), index=True, unique=True)
    body = db.Column(db.String(500), index=True)
    tags = db.relationship('Tag', secondary=tags,
        backref=db.backref('posts', lazy='dynamic'))
    search_vector = db.Column(TSVectorType)

    def __repr__(self):
        return '<Post %r %r>' % (self.title, self.tags)

class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), index=True, unique=True)

    def __repr__(self):
        return '<Tag %r>' % (self.name)

我有可搜索的列,为什么会出现此错误?我在谷歌上找不到任何东西。

1 个答案:

答案 0 :(得分:4)

您错过了__上的__searchable_columns:{/ p>

__searchable_columns = ['title', 'body']  # Invalid
__searchable_columns__ = ['title', 'body']  # Will work