![](/img/trans.png)
[英]sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed
[英]sqlalchemy.exc.IntegrityError: UNIQUE constraint failed
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) 唯一約束失敗: user.city [SQL: INSERT INTO user (username, "accountName", city, email, avatar, password) VALUES (?, ?, ?, ,?)] [參數:('sunny123','sunny kumar','Tumkur','sunny@gmail.com','default.jpg','$2b$12$n3eEOEBhJ7aVEA4wctQn4O37jEYdgI0N2jqhNKm7Giy7Y7hxwaZIS')](此錯誤的背景在: http://sqlalche.me/e/gkpj )
我的用戶 Model 是
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(20), unique = True, nullable = False)
accountName = db.Column(db.String(20), nullable = False)
city = db.Column(db.String(20), nullable = False)
email = db.Column(db.String(120), unique = True, nullable = False)
avatar = db.Column(db.String(20), nullable = False, default = 'default.jpg')
password = db.Column(db.String(60), nullable = False)
def __repr__(self):
return f"User('{self.username}', '{self.accountName}','{self.city}','{self.email}', '{self.avatar}')"`enter code here`
我的路線是
@app.route('/signup', methods = ['GET', 'POST'])
def signup():
if current_user.is_authenticated:
return redirect(url_for('home'))
form = SignUp()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user = User(username = form.username.data, accountName = form.accountName.data, city = form.city.data, email = form.email.data, password = hashed_password)
db.session.add(user)
db.session.commit()
flash('Your account has been created, now you can Signin', 'success')
return redirect(url_for('signin'))
return render_template('signup.html', title = 'Sign Up', form = form)
每當我嘗試添加具有城市名稱的用戶時,例如:以前的用戶提到的“abc”然后它會引發錯誤,但在我的用戶 model 中 unique = True 僅用於用戶名。
SQLAlchemy 作為一個庫,可以很容易地用於管理表,甚至創建它們,但僅限一次。
如果您之前有一個唯一的city
列,並且您沒有從數據庫中更改它,它將繼續加強對該列的唯一約束。
因為對於數據庫來說,它還是有唯一約束的。 您不能使用 SQLite 中的ALTER
刪除UNIQUE
約束。
請參閱此答案以安全地重新創建數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.