sqlalchemy.exc.InterfaceError: <unprintable InterfaceError object>

≡放荡痞女 提交于 2019-12-02 01:33:15

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!