簡體   English   中英

Flask: sqlalchemy.exc.OperationalError

[英]Flask: sqlalchemy.exc.OperationalError

因此,我正在嘗試創建一個擁有可以創建帖子的用戶的博客,在任何帖子中都應該有用戶撰寫的評論。

那是我的代碼:

 class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), nullable=False, unique=True)
    email = db.Column(db.String(120), nullable=False, unique=True)
    password = db.Column(db.String(60), nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    posts = db.relationship('Post', backref='author', lazy=True)
    comments = db.relationship('Comment', backref='commentauthor', lazy=True)

    def __repr__(self):
        return f"User('{self.username}','{self.email}','{self.image_file}')"

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    comments = db.relationship('Comment', backref='postcontainer', lazy=True)

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"


class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Comment('{self.content}', '{self.date_posted}')"

還有我路線上的代碼:

@app.route("/", methods=['POST', 'GET'])
def home():
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    cpost = CreatePost()
    if cpost.validate_on_submit():
        post = Post(title=cpost.title.data, content=cpost.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('home'))
    ccomment = CreateComment()
    comments = Comment.query.all()
    if ccomment.validate_on_submit():
        comment = Comment(content=ccomment.content.data)
        db.session.add(comment)
        db.session.commit()
        return redirect(url_for('home'))

    return render_template('home.html', posts=posts, cpost=cpost, ccomment=ccomment, comments=comments)

我最終得到這個錯誤:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: comment [SQL: SELECT comment.id AS comment_id, comment.date_posted AS comment_date_posted, comment.content AS comment_content, comment.post_id AS_comment_post_id AS_comment.post_id AS_comment_post_id評論](此錯誤的背景: http://sqlalche.me/e/e3q8

Traceback(最近一次通話最后一次)

我怎樣才能解決這個問題?

您需要在數據庫中創建注釋表,如果您使用的是 flask-sqlalchemy 擴展,那么您只需要調用此 function https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy。 SQLAlchemy.create_all

https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/

Go 到您的 python shell Z05B8C74CBD96FBF2DE4C1A352702FFF4Z 和類型:

from name import db
db.create_all()

name是您的項目的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM