Sqlalchemy.exc.InterfaceError: <interfaceError non-printable object>

I try to use Flask, but I get sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object> error when sending wtforms. Model class:

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

Route:

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

In my html, I import the wtf.html bulb wtf.html page:

 {{ wtf.quick_form(form) }} 

The form shows correctly, but I get the above error when submitting the form. Any advice or ideas on how to proceed will be helpful.

+2
source share
3 answers

In the table class definition, you need to add another row to end 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 had the same error message in an application that worked for one day, and not the following. The nuts pulled me out, the solution was that I deleted the relationship somewhere ().

+1
source

In def add_post() you write user_id=current_user , but that is wrong.

As you have 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 .

+1
source

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

0
source

Source: https://habr.com/ru/post/1266344/


All Articles