简体   繁体   English

当我在书评应用程序上提交评论时无法调整类型“RowProxy”错误

[英]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!提前致谢!

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!感谢大家!

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,).如果您只关注错误,则实际错误来自您希望成为 integer 但结果不是(101,)的 book_id。 Simply add [] after the value of 'book_id' ie, 'book_id': _id["book_id"] Actually what you were writing returns a dictionary.只需在 'book_id' 的值后添加 [] 即 'book_id': _id["book_id"] 实际上你正在写的返回一个字典。 Hope you will get rid of the bug.希望你能摆脱这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Flask和PostgresSQL时出错-sqlalchemy.exc.ProgrammingError:无法适应类型“ RowProxy” - Error using Flask and PostgresSQL - sqlalchemy.exc.ProgrammingError: can't adapt type 'RowProxy' 如何按评论而不是平均评论对书籍进行排序? - How can I sort books by review, but not by average review? Flask 和 sqlalchemy:发布到数据库时收到“无法调整类型‘ABCMeta’”错误 - Flask and sqlalchemy: Receiving a "can't adapt type 'ABCMeta'" error when posting to database Imdb审核编码错误 - Imdb review encoding error Django迁移错误无法适应UUID类型 - Django migration error can't adapt type UUID Django OperationalError,位于/ books / add_review_to_book - Django OperationalError at /books/add_review_to_book Django,“无法适应类型错误”,Sqlite vs Postresql - Django, “Can't adapt type error”, Sqlite vs Postresql 我可以使用sqlalchemy在RowProxy中分配值吗? - Can I assign values in RowProxy using the sqlalchemy? 如何避免ProgrammingError:在将Django模型实例保存到远程数据库时,无法调整类型'DateTimeRangeField'? - How can I avoid ProgrammingError: can't adapt type 'DateTimeRangeField' when saving a Django model instance to a remote database? /admin/app/review/ 处的 OperationalError 没有这样的列:app_review.startup_id - OperationalError at /admin/app/review/ no such column: app_review.startup_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM