我的应用程序 flask 中出现了这个错误
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'mail@gmail.com', 'pbkdf2:sha1:1000$p8jlOhqU$fa51e0491a729cef6d05dbd9f1d868455de4be9c', None, None)]
不过,我认为代码没问题。 我不知道为什么不能正常工作,允许 None 的值这样做,因为 null=True。
我的文件如下
这是我的模型.py
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)
这是我的controller.py
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)
最佳答案
当我将我的模型 id
定义为 BIGINT 时,我遇到了同样的问题,但是我的测试代码使用 sqlite
作为测试数据库,当我更改我的模型定义时,问题解决了.
关于python - NOT NULL 约束在 Flask + SQLite3 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32927742/