title: Custom Flask Validation Error not triggering getting Unique constraint error instead
body: <p>I have created a registration form with validation but instead of this error Unique Constraint error is coming how can I fix this</p>
  <p>Below is the register class</p>
  <pre><code>class Registerform(FlaskForm):
      username = StringField(label=&quot;Username&quot;, validators=[
          Length(min=3, max=20), DataRequired()])
      password1 = PasswordField(label=&quot;Password&quot;, validators=[
          Length(min=6, max=20), DataRequired()])
      password2 = PasswordField(label=&quot;Confirm Password&quot;, validators=[
          EqualTo('password1', message='Both password fields must be equal!'), DataRequired()])
      Submit = SubmitField(label='Submit')
  
      def validate_existing_user(self, username):
          checkexisting = User.query.filter_by(username=username.data).first()
          print(checkexisting)
          if checkexisting:
              raise ValidationError(
                  &quot;Oops Username already exists please try with a new Username&quot;)
  </code></pre>
  <p>Below is the registration route</p>
  <pre><code>@app.route(&quot;/register&quot;, methods=['POST', 'GET'])
  def registerpage():
      form = Registerform()
  
      if form.validate_on_submit():
          usernameinput = form.username.data
          userpasswordinput = form.password1.data
          hashedpass = bcrypt.generate_password_hash(userpasswordinput)
          print(f&quot;Normal pass is {userpasswordinput} &amp; its hash is {hashedpass}&quot;)
          table_row = User(username=usernameinput,
                           password_hash=hashedpass, )
  
          db.session.add(table_row)
          db.session.commit()
          print(&quot;User added successfully&quot;)
          flash('User added successfully you can Login now',
                category='success')
          return redirect(url_for('login_page'))
  
  
  
      return render_template(&quot;registration.html&quot;, title='Registration', form=form)
  </code></pre>
  <p>Below is the DB model its sqllite</p>
  <pre><code>class User(db.Model, UserMixin):
      id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      username = db.Column(db.String(80), unique=True, nullable=False)
      password_hash = db.Column(db.String(120), nullable=False)
  
      def __repr__(self):
          return '&lt;Username %s&gt; &lt;pwd %s&gt;' % (self.username, self.password_hash)
  </code></pre>
  <p>Kindly assist I am new to flask</p>