[英]NOT NULL constraint Failed in Flask + SQLite3
I got this error in my app flask 我的app瓶中出现此错误
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)]
However, I think that the code is fine. 但是,我认为代码很好。 I don't know why doesn't work properly, the values that are as None are allowed to do it, because null=True. 我不知道为什么不能正常工作,允许使用无值的值,因为null = True。
My files are the following 我的文件如下
This is my models.py 这是我的models.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)
And this is my controller.py 这是我的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)
You can examine the database schema by starting the SQLite shell and using the .schema
command. 您可以通过启动SQLite shell并使用.schema
命令来检查数据库模式。
$ sqlite3 app.db
sqlite> .schema user
At some point, your model had nullable=False
set on the role
column. 在某些时候,您的模型在role
列上设置了nullable=False
。 You created the database with this model, then changed it to True
. 您使用此模型创建了数据库,然后将其更改为True
。 Changing the model after the database was created does not change the database, you need to migrate it. 在创建数据库后更改模型不会更改数据库,您需要迁移它。 Use Alembic to migrate a SQLAlchemy database. 使用Alembic迁移SQLAlchemy数据库。
当我将Model id
定义为BIGINT时,我遇到了同样的问题,但我的测试代码使用sqlite
作为测试数据库,当我更改模型定义时,问题解决了。
in flask project, if U use the sqlite database, If the id in the model is set to BIGINT, an error will be occured when do db.session.commit()
: UNIQUE constraint failed: tablename.column_name
. 在flask项目中,如果U使用sqlite数据库,如果模型中的id设置为BIGINT,则db.session.commit()
: UNIQUE constraint failed: tablename.column_name
时将发生错误UNIQUE constraint failed: tablename.column_name
。
then you can resolve it by: 那你可以通过以下方式解决它:
delete the table in the database, add the attribute AUTOINCREMENT
, then recreate this table and be resolved: 删除数据库中的表,添加属性AUTOINCREMENT
,然后重新创建该表并解决:
CREATE TABLE table_name (
id BIGINT PRIMARY KEY AUTOINCREMENT NOT NULL,`
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.