Python – email being stored as NULL

Viewed 22

I am creating a web app for standalone teachers to manage their courses. When I try to register as a new user (i.e. a teacher), my email is passed as NULL to the database. Every other credential (first name, last name, password) is being passed correctly. Because I want to log in with the email, using a username is not a good solution.

I am using Python + SQLAlchemy, and I tried to use nullable=False, which (correctly) gives me IntegrityError for user.email in this case. I am fairly sure my script is okay too as other mentioned values are being passed fine. I also tried to drop and recreate the whole database but it didn't help.

Here is my database table for users (i.e. teachers):

# Creating a model that is used in the database (basically defining columns)
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(50))
    last_name = db.Column(db.String(50))
    email = db.Column(db.String(50), unique=True, nullable=False)
    password = db.Column(db.String(50))
    courses = db.relationship("Course")

Here is my register function:

@app.route("/register", methods=["GET", "POST"])
def register():
    """ Register user """
    if request.method == "POST":

        # must input first name
        if not request.form.get("first_name"):
            flash("First name is required.", category="error")

        # must input last name
        if not request.form.get("last_name"):
            flash("Last name is required.", category="error")

        # must input email
        if not request.form.get("email"):
            flash("Must input email", category="error")

        # must input password
        if not request.form.get("password"):
            flash("Must input password", category="error")

        # must input confirmation
        if not request.form.get("confirmation"):
            flash("Must input confirmation", category="error")
        
        # storing all inputs from user
        first_name = request.form.get("first_name")
        last_name = request.form.get("last_name")
        email = request.form.get("email")
        password = request.form.get("password")
        confirmation = request.form.get("confirmation")

        email = User.query.filter_by(email=email).first()
        if email:
            flash("Email is already used.", category="error")
            return render_template("register.html", user=current_user)

        if password != confirmation:
            flash("Passwords do not match.", category="error")

        new_user = User(first_name=first_name, last_name=last_name, email=email, password=generate_password_hash(password))
        db.session.add(new_user)
        db.session.commit()
        flash("Account created! Please log in.", category="success")
        return redirect("/login")

    else:
        return render_template("register.html", user=current_user)

How can I get rid of this behavior and save the emails correctly? Any help will be appreciated.

0 Answers
Related