Clementnduonyi
Clementnduonyi

Reputation: 31

can't adapt type 'RowProxy' Error when I submit review on the book review app

I am having this error:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'RowProxy'
[SQL: SELECT * FROM reviews WHERE user_id = %(user_id)s AND book_id = %(book_id)s]
[parameters: {'user_id': 1, 'book_id': (101,)}]

When I want to submit a review with the following code. I have tried to figure it out with the database schema to no avail but I feel something is not right somewhere with my code which I have not been able to pin-point since its a programming error. Here is my code:

@app.route("/book/<isbn>", methods=["GET", "POST"])
@loggedin_required
def book(isbn):
    """ Takes and renders user review on the book page."""

    if request.method == "POST":

        # Keep track of individual user per session
        active_user = session["user_id"]
        
        # Fetch data from form
        rating = request.form.get("rating")
        comment = request.form.get("comment")
        time = datetime.now()
        #active_user = session.get("user_id")
       
        
        # Get book_id by ISBN
        row = db.execute("SELECT id FROM books WHERE isbn = :isbn",
                        {"isbn": isbn})

        # Saving id into variable
        _id = row.fetchone() 
        _Id = _id[0]

        # Check to ensure that its ONLY 1 review/user per book)
        row_check = db.execute("SELECT * FROM reviews WHERE user_id = :user_id AND book_id = :book_id",
                    {"user_id": active_user,
                     "book_id": _id})

        if row_check.rowcount == 1:
            
            flash('You have left a review for this book already.')
            return redirect("/book/" + isbn)

        # Convert to save into DB
        rating = int(rating)

        db.execute("INSERT INTO reviews (rating, comment, time, book_id, user_id) VALUES \
                    (:rating, :comment, :time, :book_id, :user_id )",
                    {"rating": rating,
                      "comment": comment,
                      "time": time,
                      "book_id": _id, 
                      "user_id": active_user})

       
        db.commit()

        flash('Review recieved. Thank you!')

        return redirect("/book/" + isbn)
    
    # Take the book ISBN and redirect to his page (GET)
    else:

        row = db.execute("SELECT isbn, title, author, year FROM books WHERE \
                        isbn = :isbn",
                        {"isbn": isbn})

        book_details = row.fetchall()


        """ Goodreads user review api """
        key = os.getenv("GOODREADS_KEY")

        res= requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": key, "isbns": isbn})

        user_action = res.json()

        user_action = user_action["books"][0]

        book_details.append(user_action)

        """ Book reviews by users """

        row = db.execute("SELECT id FROM books WHERE isbn = :isbn",
                        {"isbn": isbn})
        
        book = row.fetchone()
        book = book_details[0]


        return render_template("/book.html", book_details=book_details)

Does anyone know why this error occurs? Any help would be much appreciated. Thanks in advance!

Upvotes: 0

Views: 424

Answers (2)

Clementnduonyi
Clementnduonyi

Reputation: 31

I got this. It was mainly a typo error here:

```
 _id = row.fetchone() 
 _Id = _id[0]
```

It should be
```
 _id = _id[0]

```
instead. 

Thanks everyone!

Upvotes: 1

Irfan wani
Irfan wani

Reputation: 5094

If you just focus on the error, the actual error comes from book_id which you want to be an integer but is not as the result is (101,). Simply add [] after the value of 'book_id' i.e, 'book_id': _id["book_id"] Actually what you were writing returns a dictionary. Hope you will get rid of the bug.

Upvotes: 0

Related Questions