python-sqlalchemy.exc.InterfaceError:

我正在尝试Flask,但出现错误sqlalchemy.exc.InterfaceError:< unprintable InterfaceError object>同时提交wtforms.该模型类是:

 class Post(db.Model):
__tablename__ = 'blog_posts'
id = db.Column(db.Integer, unique=True, primary_key=True)
title = db.Column(db.String(50), unique=False)
content = db.Column(db.Text, unique=False)
user_id = db.Column(db.String, db.ForeignKey('users.username'))



@staticmethod
def post_new_entry(title, content, user_id):
    """ Post new entry to database """
    new_post = Post(title=title, content=content, user_id=user_id)
    db.session.add(new_post)
    db.session.commit()
    return new_post

def __repr__(self):
    return 'PostID {}: {} by {}'.format(self.id, self.title, self.user_id)

对于我的表格,我具有以下内容:

class PostForm(Form):
title = StringField('Title', validators=[DataRequired(), Length(10, 65)])
post_content = TextAreaField('Content', validators=[DataRequired(), Length(50, 500)])
submit = SubmitField('Publish Post')

路线是:

@main.route('/new_post/', methods=['GET', 'POST'])
@login_required
def add_post():
    form = PostForm()
    if form.validate_on_submit():
        Post.post_new_entry(title=form.title.data,
                            content=form.post_content.data,
                            user_id=current_user)
        flash("Amazing stuff! Thanks for your submission.")
        return redirect(url_for('main.index'))
    return render_template('single.html', form=form)

在我的html上,我要导入flask-bootstrap的wtf.html页面:

{{ wtf.quick_form(form) }} 

表单显示正确,但提交表单时出现上述错误.关于如何进行的任何提示或想法都将有所帮助.

解决方法:

在表类定义中,您需要再添加一行以完成外键关系.

class Post(db.Model):
    __tablename__ = 'blog_posts'
    id = db.Column(db.Integer, unique=True, primary_key=True)
    title = db.Column(db.String(50), unique=False)
    content = db.Column(db.Text, unique=False)
    user_id = db.Column(db.String, db.ForeignKey('users.username'))

    # Setup the relationship to the User table
    users = db.relationship(User)

我在一个应用程序中遇到了相同的错误消息,但一天后却无法正常工作.疯了我,解决方法是我在某个地方删除了Relationship().

上一篇:如何将查询结果发送到WTForm字段?


下一篇:烧瓶-WTF:缺少CSRF令牌