0

My buy function was working when I tested it, buy yesterday, when I was testing my implementation of "index" it stopped. The "INSERT INTO portfolio" line is no longer executing properly, and I cannot figure out why. I attached my code for buy and the server log. Thank you for your help!

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""
    from time import gmtime, strftime

    if request.method == "POST":
        shares = request.form.get("shares")
        symbol = request.form.get("symbol")

        if not shares:
            return apology("PROVIDE NUMBER OF SHARES")
        if not symbol:
            return apology("PROVIDE SYMBOL")

        dictionary = lookup(symbol)

        if not dictionary:
            return apology("PROVIDE VALID SYMBOL")

        price = dictionary['price']
        symbol = dictionary['symbol']
        cash = db.execute("SELECT cash FROM users WHERE id = :userid", userid=session["user_id"])
        user = db.execute("SELECT username FROM users WHERE id = :userid", userid=session["user_id"])
        time=strftime("%Y-%m-%d %H:%M:%S", gmtime())

        cash = cash[0]["cash"]
        shares = int(shares)
        total = 0
        for i in range(shares):
            total += price
        if cash >= price:
            db.execute("INSERT INTO portfolio (User, Price, Time, Symbol, Shares, Total) VALUES (:userid, :price, :time, :symbol, :shares, :total)", userid=session["user_id"], price=price, time=time, symbol=symbol, shares=shares, total=total)
            db.execute("UPDATE users SET cash = cash - :price WHERE id = :id", price=price, id=session["user_id"])
        else:
            return apology("NOT ENOUGH CASH")
        return redirect ("/")
    else:
        return render_template("buy.html")

enter image description here

1

I have a number of questions that may answer your issue.

  1. Does it still work if you create a new user and "buy" a new stock?
  2. Does it work for an old user and a new stock that nobody has bought?
  3. Does it work for a new user and an old stock that someone else has bought?
  4. Is the user making an additional purchase of stock that they already own?
  5. What are the keys and/or unique indexes on the portfolio table? Did you make a unique index or primary key on the userid field only? Or on the combination of the userid/symbol combination? Or is there no unique key/index?

The first thought that I have is that the code isn't handling multiple purchases of the same stock. I'm guessing that an original purchase by a user works, but a subsequent purchase of the same stock fails. Also, it's possible that after the first purchase, all further purchases will fail too. Depending on the scenario, it'll tell you what to look at in the database structure.

The one thing that jumps out at me is that there's no code to handle the case where the user already owns some of the stock being bought. I'm assuming that there are certain uniqueness restrictions on the portfolio table that prevents more than one row for each combination of user and stock. Further, there would need to be some kind of "ON DUPLICATE UPDATE" clause. Of course, this depends on how you implemented it. If the uniqueness restriction is on just the user or just the stock, then that's going to cause problems too.

If this doesn't help fix the problems, try testing the various conditions in my questions above and let me know what happens.

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

1
  • Yes, the problem was that I had set the id as "Primary Key". Thank you! – WARMACHINEROX Jul 23 '18 at 4:55

You must log in to answer this question.

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