i am currently building a forum section into my Website using Python Flask und SQL-ALCHEMY
I have created some db Models in my models.py witch looks like this:
from .import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=False)
content = db.Column(db.Text, nullable=False)
date_created = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey(
'user.id'))
comments = db.relationship(
'Comment', backref='post', cascade="all, delete-orphan")
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
date_created = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey(
'user.id'))
post_id = db.Column(db.Integer, db.ForeignKey(
'post.id'))
user = db.relationship('User', backref='author', lazy=True)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(150))
posts = db.relationship('Post', backref='author', lazy=True)
comments = db.relationship('Comment', backref='author', lazy=True)
my Forum templates look like this
Forum.html:
{% extends "base.html" %} {% block title %}Forum{% endblock %} {% block content
%}
<h2>Forum</h2>
<a href="{{ url_for('forum.create_post') }}" class="btn btn-primary"
>Create Post</a
>
<ul>
{% for post in posts %}
<li>
<a href="{{ url_for('forum.post_detail', post_id=post.id) }}"
>{{ post.title }}</a
>
- by {{ post.author.first_name }}
</li>
{% endfor %}
</ul>
{% endblock %}
create_post.html
{% extends "base.html" %} {% block title %}Create Post{% endblock %} {% block
content %}
<h2>Create a New Post</h2>
<form method="POST">
<input type="text" name="title" placeholder="Title" required />
<textarea
name="content"
placeholder="Write your post here..."
required
></textarea>
<button type="submit">Post</button>
</form>
{% endblock %}
post_detial.html
{% extends "base.html" %} {% block title %}{{ post.title }}{% endblock %} {%
block content %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<p>By: {{ post.author.first_name }}</p>
<h3>Comments</h3>
<ul>
{% for comment in comments %}
<li>{{ comment.content }} - by {{ comment.author.first_name }}</li>
{% endfor %}
</ul>
{% if current_user.is_authenticated %}
<form method="POST">
<textarea name="comment" placeholder="Add a comment..." required></textarea>
<button type="submit">Comment</button>
</form>
{% else %}
<p>You must be logged in to comment.</p>
{% endif %} {% endblock %}
my problem is the Following:
Although a creation of the Post is working, I get a Error Message if i click on the Posts title which is "test" (see Image):
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: comment.content
[SQL: SELECT comment.id AS comment_id, comment.content AS comment_content, comment.date_created AS comment_date_created, comment.user_id AS comment_user_id, comment.post_id AS comment_post_id
FROM comment
WHERE comment.post_id = ?]
[parameters: (1,)]
(Background on this error at: )
[enter image description here](.png)
Thank you for your help in Advance, i really dont know how to proceed
I tried migration flask, to update the db, but it didnt seem to work