0

尝试将用户名和密码插入 PostgreSQL 数据库表时,出现以下错误:

TypeError
TypeError: can't pickle _thread._local objects

Traceback (most recent call last)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 1833, in finalize_request
response = self.process_response(response)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask\app.py", line 2114, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\flask_session\sessions.py", line 355, in save_session
total_seconds(app.permanent_session_lifetime))
File "c:\users\alex\appdata\local\programs\python\python37-32\lib\site-packages\werkzeug\contrib\cache.py", line 839, in set
pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
TypeError: can't pickle _thread._local objects

我不确定这意味着什么,但假设这是导致问题的代码:

user_id = db.execute("INSERT INTO users (username, password) VALUES(:username, :password)", {"username": username, "password": password})

if not user_id:
   return render_template("error.html", message="username already taken")

# stores users id
session["user_id"] = user_id

return redirect("/")

我的数据库有三个字段:用户名(文本)、密码(文本)和 user_id(整数)

在我的 python 文件中,application.py 使用以下代码将我的烧瓶应用程序连接到数据库。

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine)) 
4

1 回答 1

1

我知道这已经很晚了,但以防万一有人仍然遇到这个问题,我想你忘了包括fetchone()or fetchall()。所以 user_id 行应该user_id = db.execute("SELECT user_id FROM users WHERE username = :username", {"username": username}).fetchone()是这样的,假设你对表中的每个用户都有一个唯一的用户 ID。

同样,我相信您会希望db.commit()在尝试将行插入表中的任何行之后运行。

会话行应为session["user_id"] = user_id[0]. 希望这可以帮助。

于 2020-01-15T21:38:04.153 回答