flask db.session.commit()抛出 InvalidRequestError?-问答-阿里云开发者社区-阿里云

开发者社区> a123456678> 正文

flask db.session.commit()抛出 InvalidRequestError?

2016-06-29 11:05:25 3292 1

在python中运行db.session.commit()抛出

`Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\wtf\myenv\lib\site-packages\sqlalchemy\orm\scoping.py", line 157, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "C:\Users\wtf\myenv\lib\site-packages\sqlalchemy\orm\session.py", line 801, in commit
    self.transaction.commit()
  File "C:\Users\wtf\myenv\lib\site-packages\sqlalchemy\orm\session.py", line 390, in commit
    self._assert_active(prepared_ok=True)
  File "C:\Users\wtf\myenv\lib\site-packages\sqlalchemy\orm\session.py", line 214, in _assert_active
    % self._rollback_exception
InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (sqlite3.OperationalError) table users has no column named role_id [SQL: u'INSERT INTO users (username, role_id) VALUES (?, ?)'] [parameters: ('join', 1)]`
xuanran.py

from flask import Flask, render_template, session, redirect,url_for, flash
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from datetime import datetime
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from flask.ext.sqlalchemy import SQLAlchemy
import os


basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = \
    "sqlite:///" + os.path.join(basedir, "data.sqlite")
app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] = True
db = SQLAlchemy(app)
app.config["SECRET_KEY"] = "hard to guess string"
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
now = datetime.now()

class NameForm(Form):
    name = StringField("What is your name ?", validators = [Required()])
    submit = SubmitField("Submit")
class Role(db.Model):
    __tablename__ = "roles"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role')
    def __repr__(self):
        return "<Role %r>" % self.name
class User(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    def __repr__(self):
            return "user %r" % self.username
            
@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get("name")
        if old_name is not None and old_name != form.name.data:
            flash ("Looks like you have changed your name !")
        session["name"] = form.name.data
        return redirect(url_for("index"))
    return render_template('index.html', form=form, name=session.get('name'))    
    
@app.route("/user/<name>")
def user(name):
    return render_template("user.html", name = name )
    
@app.errorhandler(404)
def page_not_found(e):
    return render_template("404.html"), 404
    
if __name__ == "__main__":    
    app.run(debug = True)
    manager.run()

求教!

取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:48:54

    users表中的role_id项是后加上去的吧?需要用migrate做个数据库迁移才可以。

    0 0
相关问答

4

回答

Spark 【问答合集】

社区小助手 2019-05-29 14:13:40 123730浏览量 回答数 4

17

回答

【大咖问答】对话PostgreSQL 中国社区发起人之一,阿里云数据库高级专家 德哥

阿里ACE 彭飞 2019-07-10 09:36:10 970914浏览量 回答数 17

38

回答

[@饭娱咖啡][¥20]对于慢sql有没有什么比较实用的诊断和处理方法?

江小白太白 2018-10-30 18:47:38 141680浏览量 回答数 38

22

回答

爬虫数据管理【问答合集】

我是管理员 2018-08-10 16:37:41 146799浏览量 回答数 22

31

回答

[@倚贤][¥20]刚学完html/css/js的新手学习servlet、jsp需要注意哪些问题?

弗洛伊德6 2018-10-27 21:52:43 145638浏览量 回答数 31

2

回答

mySQL数据库报错You have an error in your SQL syntax

落地花开啦 2016-02-14 16:09:24 126607浏览量 回答数 2

8

回答

flink sql 支持checkpoints吗?

游客izljdlkgbdwfc 2019-07-10 17:46:37 121172浏览量 回答数 8

42

回答

【精品问答集锦】Python热门问题

小六码奴 2019-05-30 15:27:34 135568浏览量 回答数 42

249

回答

阿里云LNAMP(Linux + Nginx + Apache + MySQL + PHP)环境一键安装脚本

云代维 2014-02-14 15:26:06 302616浏览量 回答数 249

24

回答

【精品问答】python技术1000问(1)

问问小秘 2019-11-15 13:25:00 471465浏览量 回答数 24
+关注
0
文章
14879
问答
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载