sqlalchemy.exc.InterfaceError:< unprintable InterfaceError object> [英] sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object>

查看:1047
本文介绍了sqlalchemy.exc.InterfaceError:< unprintable InterfaceError object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ p $ 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):
将新条目发布到数据库
new_post =发布(title = title,content = content,user_id = user_id)
db.session.add(new_post)
db.session.commit()
返回new_post

def __repr __(self):
return'PostID {格式(self.id,self.title,self.user_id)



<对于我的表单,我有以下几点:$ b​​
$ b $ p $ 类PostForm(Form):
title = StringField(' Title',validators = [DataRequired(),Length(10,65)])
post_content = TextAreaField('Content', validators = [DataRequired(),Length(50,500)])
submit = SubmitField('Publish Post')

这条路线是:

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


在我的html中,我正在导入flask-bootstrap的wtf.html:

< pre $ {{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'))

#Setu p与User表的关系
users = db.relationship(User)

在有一天工作,然后不是下一个的应用程序中有相同的错误信息。驱使我坚果,解决办法是,我已经删除了一个关系()的地方。


I'm trying out Flask but I'm having the error sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object> while submitting a wtforms. The model class is:

 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)

For my Form, I have the following:

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

The route is:

@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)

On my html, I'm importing the wtf.html of the flask-bootstrap:

{{ wtf.quick_form(form) }} 

The form shows right but I get the above error on form submission. Any tip or idea on how to proceed would be helpful.

Thanks

解决方案

In your table class definition you need to add one more line to complete the foreign key relationship.

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)

I was having the same error message in an app which was working one day then not the next. Drove me nuts, the solution was that I had removed a relationship() somewhere.

这篇关于sqlalchemy.exc.InterfaceError:&lt; unprintable InterfaceError object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆