Python, Postgres SQLAlchemy help. Server breaks

Hey guys, I’m currently working on a web app using Python, Postgres and SQLAlchemy(Psycopg2).

I’ve set up a form that stores information input by the user into the database, sends them an email letting them know said message was receives successfully and notifies me also.

The problem I’m running into is with duplicate values, I’ve been able to stop the server from breaking up in debug when a duplicate value is inserted into the database and instead relay a message on the screen that the information the user is entering has already been logged by the system. However I’ve added more values to the form – first name, last name etc…

The error is being thrown again. I’m thinking of storing the (input)values from the form in an array, asking the server to check that array for duplicates in the database before there’s any injection attempt made on the servers behalf.

In short the everything works great, as long as there isn’t duplicate information being inserted. Would you guys say this is a good way to handle it? If not what’s the best way to go about it? Here’s the code with the if statement that was working prior.

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from send_email import send_email

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = '|||||||||||||||||||||||||||||||||||'
db = SQLAlchemy(app)

class Data(db.Model):
    __tablename__ = 'data'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(50), unique=True)
    first_name = db.Column(db.String(50), unique=False)
    last_name = db.Column(db.String(50), unique=False)
    phone_number = db.Column(db.String(50), unique=True)

    def __init__(self, email, first_name, last_name, phone_number): = email
        self.first_name = first_name
        self.last_name = last_name
        self.phone_number = phone_number

Right here
def index():
return render_template(‘index.html’)

@app.route('/success', methods=['POST'])
def success():
    if request.method=='POST':
        last_name = request.form["last_name"]
        phone_number = request.form["phone_number"]
        send_email(email, first_name, last_name, phone_number)

        if db.session.query(Data).filter(, first_name == email).count() == 0:
            data = Data(email, first_name, last_name, phone_number)
            return render_template('success.html')
    return render_template('index.html',
    text ="This information already exist." )

if __name__ == "__main__":

Heres the email function.

from email.mime.text import MIMEText
import smtplib

def send_email(email, first_name, last_name, phone_number):
from_email = "||||||||||||||||||||||"
from_password = "|||||||||||||||||"
to_email = email

subject = "Shogun Mail List"
message = "Thanks for signing up for our mail list, we'll notify you when the boards are in"
msg = MIMEText(message, 'html')
msg['Subject'] = subject
msg['To'] = to_email
msg['From'] = from_email
gmail = smtplib.SMTP('', 587)
gmail.login(from_email, from_password)