Why is user_id column NULL? sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: comment.user_id

Why is user_id column NULL? sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: comment.user_id

我正在尝试使用 Flask-SQLalchemy 向 SQLite 数据库中的 table 提交一个 WTF 表单。但是,我收到以下错误:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: comment.user_id

我理解这意味着 Flask 应用程序当前正在尝试将 NULL 放入不可为空的列中。虽然我不明白为什么会发生这种情况,因为逻辑是从另一种形式复制的,post,它工作得很好,即 User.id 被插入到 Post.user_id 中。

我的目标是将user.id插入comments.user_id
我怎样才能做到这一点?是什么导致了当前的问题?
代码:
models.py

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

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    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)

# THIS HERE IS THE CLASS THAT I AM EXPERIENCING THIS ERROR WITH
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    comment = db.Column(db.Text, nullable=False)
    tag_1 = db.Column(db.String(50), nullable=True)
    tag_2 = db.Column(db.String(50), nullable=True)
    tag_3 = db.Column(db.String(50), nullable=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

routes.py

@tools.route("/toolkit/comments/new", methods=['GET', 'POST'])
@login_required
def add_comment():
    form = comment_form()
    if form.validate_on_submit():
        comment = Comment(comment=form.comment.data, tag_1=form.tag_1.data, tag_2=form.tag_2.data, tag_3=form.tag_3.data)
        db.session.add(comment)
        db.session.commit()
        flash('Your comment has been added!', 'success')
        return redirect(url_for('tools.comments', username=user))
    return render_template('add_comments.html', form=form)

提前致谢。

这一行:

comment = Comment(comment=form.comment.data, tag_1=form.tag_1.data, tag_2=form.tag_2.data, tag_3=form.tag_3.data)

缺少参数 user_id 对吗?只需为该参数提供所需的值即可。