Negative
Negative

Reputation: 21

sqlalchemy.exc.OperationalError in Flask

Hello I am a new programmer and I just started making websites with flask, during my most recent project I had this problem with the database(Name of error is the title ;) ) This is my code ( I am making a mini youtube)

@app.route("/create_acc", methods=["POST", "GET"])
def create_acc():
    if request.method == "POST":
        name = request.form["nm"]

        check = ChannelDB.query.filter_by(name=name).first()

    if not check:
        email = request.form["em"]
        psw = request.form["ps"]

        channel = ChannelDB(name, email, psw, 0, 0)
        db.session.add(channel)
        db.session.commit()

        return redirect(url_for("user"))
    else:
        abort(409, message="Video alredy exists")

This is where the error is accruing.

class ChannelDB(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(100), nullable=False)
subs = db.Column(db.Integer, nullable=False)
num_of_videos = db.Column(db.Integer, nullable=False)

This is my database.

{% extends "base.html" %}
{% block title %}Login Page{% endblock %}
{% block content %}
    <form action="/create_acc", method="post">
        <p>Name: </p>
        <p><input type="text" name="nm"></p>
        <p>Email: </p>
        <p><input type="text" name="em"></p>
        <p>Password: </p>
        <p><input type="text" name="ps"></p>
        <p>Press this button when you fill the spots above</p>
        <p><input type="submit" value="submit"></p>
    </form>
{% end block %}

This is my login . html file

Upvotes: 1

Views: 66

Answers (2)

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

You may need to migrate your tables into the database. If you follow the flask_sqlalchemy quickstart, you can add your db schema with the command line operation python manage.py db migrate.

Upvotes: 0

Sunil Nair
Sunil Nair

Reputation: 61

A full extract of the error would have been more conclusive. Nevertheless please try if you are able to add a record in the table using the terminal.

from app import *
channel = ChannelDB("Sample name", "sample email", "pass", 0, 0)
db.session.add(channel)
db.commit()

If the above command does not execute, drop the tables and recreate them.

from app import db
db.drop_all()
db.create_all()

If not, try recreating the tables

Upvotes: 1

Related Questions