0

I’m encountering the following error when trying to insert a comment into my SQLite database:

Error says "table comment has no column named data" apparently meaning that the data column doesn’t exist in the comment table, but I have defined it in my model. I appreciate any help given. Thank you for your time. Here’s the relevant code:

views.py:

from flask import Blueprint, render_template, request, flash, jsonify, redirect, url_for
from flask_login import login_required, current_user
from .models import Note, Comment
from . import db
from datetime import datetime
import json

views = Blueprint('views', __name__)

@views.route('/comments/<int:note_id>', methods=['GET', 'POST'])
def comment_section(note_id):
    if request.method == 'POST':
        note = Note.query.get(note_id)
        data = request.form.get('comment')
        print(data)
        if data:
            comment = Comment(data=data, note_id=note_id, user_id=current_user.id)
            db.session.add(comment)
            db.session.commit()
            flash('Comment was added successfully!', category='success')
    return render_template('comments.html', user=current_user, note=note)

models.py:

from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
from datetime import datetime

class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.String(10000))
    date = db.Column(db.DateTime(timezone=True), default=func.now())
    user = db.relationship('User')
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

class User(UserMixin, db.Model):
    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))
    note = db.relationship('Note')
    liked = db.relationship(
        'PostLike',
        foreign_keys='PostLike.user_id',
        backref='user', lazy='dynamic')

class PostLike(db.Model):
    __tablename__ = 'post_like'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('note.id'))

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.String(10000), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False)
    note_id = db.Column(db.Integer, db.ForeignKey('note.id', ondelete='CASCADE'), nullable=False)


4
  • 1
    I'm going to guess that when you originally created the database, the Comment model did not have a data column. You must have added that to the code later. Commented Jan 31 at 2:13
  • 1
    If you change the definition of a model, the database is not automatically updated to reflect the new column structure. You need to do a database migration. Commented Jan 31 at 2:14
  • 1
    Or, if you don't care about preserving the current data, you can delete the entire database and recreate it from scratch, which will use the latest model definitions. Commented Jan 31 at 2:15
  • To build on @JohnGordon's answer the commands for database migration are: flask db migrate and flask db upgrade to make the changes in the database. Hope this helps. Commented Jul 5 at 12:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.