title: Validations in SQLAlchemy
body: <p>I'm using Flask and flask-SQLAlchemy extension to make a webapp and I've added fields like <code>unique=True</code> in the model declarations. </p>
  
  <pre><code>class Coupon(db.Model):
      username = db.Column(db.String(80), primary_key=True)
      value = db.Column(db.String(80), unique=True)
      is_valid = db.Column(db.Boolean)
  
      def __init__(self, value, username):
          self.value = value
          self.username = username
          self.is_valid = True
  </code></pre>
  
  <p>What is the best(pythonic) way of recovering with a failed validation. For example -</p>
  
  <pre><code>c1 = Coupon("same_value", "foo")
  db.session.add(c1)
  c2 = Coupon("same_value", "bar")
  db.session.add(c2)
  db.session.commit() #gives an IntegrityError
  </code></pre>
  
  <p>Should I use a try, except block to deal with this? Or is there a better way to deal with transcations that have not succeeded. One additional query regarding transactions - As long as keep adding objects to  a session and not do a <code>session.commit</code> is it all a part of one transcation?</p>
  
  <p>Thanks</p>