title: Integrity Error - Flask & SQLAlchemy
body: <p>This is my first time playing around with SQLAlchemy and I'm trying to just insert an item into a table and have flask print out that item. However, I keep on getting an integrity error like this when I don't catch the exception: </p>
  
  <pre><code>sqlalchemy.exc.IntegrityError: (IntegrityError) column email is not unique u'INSERT INTO user (username, email) VALUES (?, ?)' ('test', 'test@gmail.com')
  </code></pre>
  
  <p>I have the following code:</p>
  
  <pre><code>from flask import Flask
  from flask.ext.sqlalchemy import SQLAlchemy
  from sqlalchemy.exc import IntegrityError
  
  app = Flask(__name__)
  app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
  
  db = SQLAlchemy(app)
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(80), unique=True)
      email = db.Column(db.String(120), unique=True)
  
      def __init__(self, username, email):
          self.username = username
          self.email = email
  
      def __repr__(self):
          return "&lt;User %r&gt;" % self.username
  
  @app.route("/")
  def index():
      return User.query.all() # I don't think this will work
  
  if __name__ == "__main__":
      db.create_all()
      try:
          x = User("test", "test@gmail.com")
          db.session.add(x)
          db.session.commit()
          print "worked"
      except IntegrityError:
          print "Not Working"
  
      app.run(debug=True)
  </code></pre>
  
  <p>Would anyone mind giving a hand? I've done some research on IntegrityError but most of the documentation is on handling the error and not preventing it.</p>