我的应用程序烧瓶中出现此错误
IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed:
auth_user.role [SQL: u'INSERT INTO auth_user (username, email, password, role, status) VALUES (?, ?, ?, ?, ?)']
[parameters: (u'Natali', u'[email protected]', 'pbkdf2:sha1:1000$p8jlOhqU$fa51e0491a729cef6d05dbd9f1d868455de4be9c', None, None)]
但是,我认为代码是好的。
我不知道为什么不能正常工作,因为空值等于真,所以不允许使用空值。
我的档案如下
这是我的模特
from app import db
from werkzeug import generate_password_hash, check_password_hash
class User(db.Model):
__tablename__ = 'auth_user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(128), nullable=False)
email = db.Column(db.String(128), nullable=False,
unique=True)
password = db.Column(db.String(192), nullable=False)
role = db.Column(db.SmallInteger, nullable=True)
status = db.Column(db.SmallInteger, nullable=True)
def __init__(self, username, email, password):
self.username = username.title()
self.email = email.lower()
self.password = generate_password_hash(password)
def __repr__(self):
return '<User %r>' % (self.name)
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
这是我的控制器
from flask import (
Blueprint,
request,
render_template,
flash,
g,
session,
redirect,
url_for
)
from werkzeug import check_password_hash, generate_password_hash
from app import db
from app.authentication.forms import LoginForm, SignupForm
from app.authentication.models import User
mod_auth = Blueprint('auth', __name__, url_prefix='/auth')
@mod_auth.route('/profile')
def profile():
if 'email' not in session:
return redirect(url_for('signin'))
user = User.query.filter_by(email = session['email']).first()
if user is None:
return redirect(url_for('signin'))
else:
return render_template('authentication/profile.html')
@mod_auth.route('/signup/', methods=['GET', 'POST'])
def signup():
form = SignupForm()
if 'email' is session:
return redirect(url_for('profile'))
if request.method == 'POST':
if form.validate() == False:
return render_template("authentication/signup.html", form=form)
else:
new_user = User(form.username.data, form.email.data, form.password.data)
db.session.add(new_user)
db.session.commit()
session['email'] = new_user.email
return "Not found"
elif request.method == 'GET':
return render_template("authentication/signup.html", form=form)
@mod_auth.route('/signin/', methods=['GET', 'POST'])
def signin():
form = LoginForm(request.form)
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and check_password_hash(user.password, form.password.data):
session['user_id'] = user.id
flash('Welcome %s' % user.name)
return redirect(url_for('auth.home'))
flash('Wrong email or password', 'error-message')
return render_template("authentication/signin.html", form=form)
最佳答案
您可以通过启动sqlite shell并使用.schema
命令来检查数据库模式。
$ sqlite3 app.db
sqlite> .schema user
在某个时刻,您的模型在
nullable=False
列上设置了role
。您使用此模型创建了数据库,然后将其更改为True
。创建数据库后更改模型不会更改数据库,您需要迁移它使用Alembic迁移SQLAlchemy数据库。关于python - NOT NULL约束在Flask + SQLite3中失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32927742/