sqlalchemy.exc.OperationalError psycopg2.OperationalError

Question:

I want to create a database to insert data. I have an error message.
According to the documentation, the error may come from the database itself. Has anyone ever encountered this problem?

# créer un objet SQLAlchemy pour notre application app
db= SQLAlchemy(app)

#accedder à la class Model à partir de l'objet db
class Data(db.Model):
    __tablename__ = "data"
    id=db.Column(db.Integer, primary_key=True)
    email_=db.Column(db.String(120), unique=True)
    nom=db.Column(db.Integer)

    def __init__(self, email_, nom):
        self.email_=email_
        self.nom=nom

@app.route("/")
def index():
    return render_template("index.html")


@app.route("/success", methods=['POST'])
def success():
    # récupérer les données passer au serveurs par le formulaire
    if request.method == 'POST':

        email = request.form["email_name"]
        height = request.form["height_name"]

        # On interroge le model de la base de donnee Data, on filtre sur le mail pour vérifier si il existe déja
        if db.session.query(Data).filter(Data.email_==email).count() == 0 :
            data = Data(email, height)
            db.session.add(data)
            db.session.commit()
            average_height = db.session.query(func.avg(data.nom)).scalar()
            average_height = round(average_height, 1 )
            count = db.session.query(Data.nom).count()
            send_email(email, height, average_height, count)
            print(average_height)
            return render_template("success.html")
        return render_template('index.html', text="L'adresse email existe déja ! ")


if __name__ == "__main__":

    app.debug == True
    app.run()

Error :

  sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 
  (Background on this error at: https://sqlalche.me/e/14/e3q8)
Asked By: amaranaitsaidi

||

Answers:

Seems like database data_collector is not created on your machine. After creating the DB, you can also include this in the app file:

if __name__ == "__main__":
    db.create_all()
    app.debug == True
    app.run()
Answered By: Olasimbo Arigbabu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.