sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object>
Asked Answered
B

4

5

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 page 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.

Brambling answered 27/4, 2015 at 6:30 Comment(2)
Unfortunately. Couldn't spend enough time on it.Brambling
which SQL engine did you use?Piceous
E
3

Under def add_post() you write user_id=current_user, but that's not right.

Since you defined for class Post:

user_id = db.Column(db.String, db.ForeignKey('users.username'))

in def add_post() you should use user_id=current_user.username.

Eliason answered 24/6, 2017 at 19:26 Comment(0)
B
1

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.

Bolan answered 6/1, 2016 at 20:0 Comment(1)
which SQL engine did you use?Piceous
M
0

I have received a similar message when writing data from my application to a database. This is due to the fact that the data that is written from the application needs to have the same format as a defined in the database a db.Column(db.String()) data type cannot have a list as input for example, or any other form.data. You need to use ``str()``` in these cases to prevent this error.

Mewl answered 27/5, 2019 at 7:39 Comment(0)
B
0

I think your problem came from this area: Post.post_new_entry(title=form.title.data, content=form.post_content.data, user_id=current_user) Try to be specific and do it this way: Post.post_new_entry(title=form.title.data, content=form.post_content.data, user_id=current_user.id)

Brandabrandais answered 13/12, 2020 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.