数据模型:
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(60), unique=True)
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True)
edit_time = db.Column(db.DateTime, default=timestamp)
author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
author = db.relationship('Author', back_populates='posts')
category = db.relationship('Category', back_populates='posts')
视图函数:
@app.route('/new-post', methods=['POST', 'GET'])
@login_required
def new_post():
form = PostForm()
if form.validate_on_submit():
new_post = Post(
title=form.title.data,
category=Category.query.get(form.category.data),
body=form.body.data,
author=current_user
)
db.session.add(new_post)
db.session.commit()
flash('文章发布成功!', 'success')
return safe_redirect()
return render_template('new_post.html', form=form)
错误信息:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: post.timestamp [SQL: 'INSERT INTO post (title, body, timestamp, edit_time, author_id, category_id) VALUES (?, ?, ?, post.timestamp, ?, ?)']
问题:
我在数据模型里 明明有 timestamp 这个字段,为什么还是要提示没有?
我使用 mariadb 数据库就完全没有问题!