I've looked at lots of other questions with people who have code for register, and my SQL code is exactly the same as most of them, but when I register, it lets multiple people have the same username. I don't know why this is, but really need some help.

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""
    if request.method == "POST":
        if not request.form.get("username"):
            return apology("must provide username!")
        elif not request.form.get("password"):
            return apology("must provide password")
        elif not request.form.get("password_again"):
            return apology("must provide password confirmation")
        elif request.form.get("password") != request.form.get("password_again"):
            return apology("Sorry, passwords didn't match. Try again.")
        hash = generate_password_hash(request.form.get("password"), method='pbkdf2:sha256', salt_length=8)
        result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username = request.form.get("username"), hash = hash)
        if not result:
            return apology("That username has already been taken.")
        rows = db.execute("SELECT * FROM users WHERE username = :username", username = request.form.get("username"))
        session["user_id"] = rows[0]["id"]
        db.execute("INSERT INTO stocks (user_id) VALUES (:user_id)", user_id=rows[0]["id"])
        return redirect("/")
        return render_template("register.html")

2 Answers 2


Iirc the users table in the db was provided for us, so while Cliff's solution would work it's not the issue.

I'm not sure if the SQL in the below function is calling anything, so nothing will be assigned to result. You'll just insert new lines without ever checking for duplicates.

result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username = request.form.get("username"), hash = hash)

My solution to username checking was different from yours. I looked at the code provided to check for a valid username in the login function and edited it so it ensured unique usernames were provided for registering.

  • Looking at the login code fixed my problem.
    – mkg15
    Commented Aug 19, 2020 at 23:44

This is usually an issue with the database itself and not with the code. When you created the table, did you create either a primary or unique key on the username column? This is what prevents duplicate names and will return an error to the sql if an attempt is made to insert a duplicate name.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • It says RuntimeError: UNIQUE constraint failed: users.username, with an internal server error apology on the website instead of an apology saying that the username has already been taken. Is that a problem with this method? If so, is there a way to avoid it?
    – mkg15
    Commented Aug 18, 2020 at 23:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .