Search code examples
postgresqlflaskflask-sqlalchemyheroku-postgresadminer

postgresql: how to store id in session


I'm using postgreSQL with Python (using Flask and SQLAlchemy as ORM), and I am implementing a website which requires the user to register and log in, after registering I want the user to automatically log in, for this I used:

# storing id in session  
rows = db.execute("SELECT * FROM users WHERE username = :username ",
{"username": request.form.get("username")})
    session["user_id"] = rows[0]["id"]

# Redirect user to home page
    return redirect("/")  

but it gives a error like:

TypeError: 'ResultProxy' object does not support indexing  

So I am stuck how to do it.


Solution

  • It means that you should call fetchall or fetchone method on the result proxy:

    rows = db.execute('query').fetchall()
    session["user_id"] = rows[0]["id"]