Skip to content Skip to sidebar Skip to footer

Sqlalchemy.exc.InterfaceError:

I'm trying out Flask but I'm having the error sqlalchemy.exc.InterfaceError: while submitting a wtforms. The model class is: class Post(d

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


Solution 2:

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.


Solution 3:

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.


Solution 4:

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)


Post a Comment for "Sqlalchemy.exc.InterfaceError: "