Could someone help me figure out which values should I define as UNIQUE? I'm getting the ValueError: UNIQUE constraint failed: compras.id beacuase of the way I created my table for keeping tab of who bought what and when for finance of pset9. Thank you so much in advance and also for reading
CREATE TABLE users (id INTEGER, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00, PRIMARY KEY(id));
CREATE UNIQUE INDEX username ON users (username);
def buy():
"""Buy shares of stock"""
if request.method == "POST":
user_id = session["user_id"]
symbol = request.form.get("symbol")
result = lookup(symbol)
if not result:
return apology("The symbol does not exist",403)
shares = request.form.get("shares")
price = result["price"]
product = (result["price"]*int(shares))
#if shares < 0:
# return apology("You forgot to type a positive integer.",403)
cash = db.execute("SELECT cash FROM users WHERE id = ?", user_id)[0]['cash']
# okay if avilableCash is a list of zero or more dict objets, which are its key and values ?
alcanza = cash - product # for some reason it worked like this lol
if alcanza > 0:
ts = datetime.datetime.now().timestamp()
db.execute("INSERT INTO compras (id,symbol,shares,price,date) VALUES(?,?,?,?,?)",user_id, symbol, shares, price,ts)
return redirect("/")
else:
return apology("not enough money",403)
else:
return render_template("buy.html")