Datetime 烧瓶SQLAlchemy日期时间utcnow

Datetime 烧瓶SQLAlchemy日期时间utcnow,datetime,flask,flask-sqlalchemy,Datetime,Flask,Flask Sqlalchemy,我想创建一个博客web应用程序,但是当通过db.session.commit()提交评论时,它将更改表posts中的时间字段。我真正想做的是,发帖的时间是发帖的时间,它不会随着评论的提交而改变。 这是我的密码: class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True, nullable=False) time = db.Column(db.DateT

我想创建一个博客web应用程序,但是当通过
db.session.commit()
提交评论时,它将更改表
posts
中的
时间
字段。我真正想做的是,发帖的时间是发帖的时间,它不会随着评论的提交而改变。 这是我的密码:

class Post(db.Model):
    __tablename__ = 'posts'
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    time = db.Column(db.DateTime, index=True, nullable=False, default=datetime.utcnow)
    text = db.Column(db.Text, nullable=False)
    num_of_comments = db.Column(db.Integer, index=True, default=0)    
    comments = db.relationship('Comment', backref='post', lazy='dynamic')

class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
    text = db.Column(db.Text)

@main.route('/post/<int:id>', methods=['GET', 'POST'])
def post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()

    if form.validate_on_submit():
      comment_author = current_user._get_current_object()
      comment = Comment(text = form.text.data
                  post=post,
                  author=comment_author)
      db.session.add(comment)
      if post.num_of_comments == None:
        post.num_of_comments = 0
      post.num_of_comments += 1
      flash('Your comment has been submitted.')
      return redirect(url_for('.post', id=post.id))

    comments = post.comments.order_by(Comment.timestamp.desc())
    return render_template('post.html', posts=[post], form=form, comments=comments)
class Post(db.Model):
__tablename_uu='posts'
id=db.Column(db.Integer,primary_key=True,nullable=False)
time=db.Column(db.DateTime,index=True,nullable=False,default=DateTime.utcnow)
text=db.Column(db.text,null=False)
注释数=db.Column(db.Integer,index=True,default=0)
comments=db.relationship('Comment',backref='post',lazy='dynamic')
类注释(db.Model):
__tablename_uu='comments'
id=db.Column(db.Integer,主键=True)
timestamp=db.Column(db.DateTime,index=True,default=DateTime.utcnow)
author\u id=db.Column(db.Integer,db.ForeignKey('users.id'))
post_id=db.Column(db.Integer,db.ForeignKey('posts.id'))
text=db.Column(db.text)
@main.route('/post/',方法=['GET','post'])
def post(id):
post=post.query.get\u或\u 404(id)
form=CommentForm()
if form.validate_on_submit():
注释作者=当前用户。获取当前对象()
注释=注释(text=form.text.data
post=post,
作者=评论(作者)
db.session.add(注释)
如果post.num_of_comments==无:
post.num_of_comments=0
post.num_of_comments+=1
flash('您的评论已提交')
返回重定向(url_for('.post',id=post.id))
comments=post.comments.order_by(Comment.timestamp.desc())
返回呈现模板('post.html',posts=[post],form=form,comments=comments)
每次按1添加
post.num\u注释
add时,它都会更改相应的post,并
db.session.commit()
更改,这将导致
post.time的更改。我应该如何避免改变


任何帮助都将不胜感激!!非常感谢

web应用程序的问题不在于烧瓶部分,而在于mysql部分: mysql中Post的时间字段定义为“更新时”,要解决此问题,您可以使用以下命令更改该列:

ALTER TABLE posts CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
现在,当您在POST中执行ShowColumns时;在mysql中,它将不再显示“更新中”

我从和得到了答案
无论如何,谢谢你们

您在哪里调用
db.session.commit()
?是否有任何数据库触发器可能正在更新POST.time?每次更改后,db都会更新更改。我可以这样做:在config.py中添加SQLALCHEMY_COMMIT_ON_TEARDOWN=True。我希望保留Post.time发布时间,而不是更改Post的时间。