I am developing an application with flask, mysql, and sqlalchemy.
The moment I try to insert a duplicate data in a UNIQUE column the app crashes. Is there any way to catch this exception in a try except like this?
if form.validate_on_submit():
hashed_password = generate_password_hash(form.password.data, method='sha256')
# write data on DB
try:
new_user = User(name=form.name.data, surname=form.surname.data, email=form.email.data,
pwd_hash=hashed_password, is_active=False, urole=form.urole.data)
db.session.add(new_user)
db.session.commit()
return '<h1>Welcome, You are a new user!</h1>'
except IntegrityError:
return '<h1>This email alredy exists!</h1>'