gpt4 book ai didi

python - flask 中的 sqlalchemy.orm.exc.UnmappedInstanceError

转载 作者:太空狗 更新时间:2023-10-29 16:56:13 24 4
gpt4 key购买 nike

我一直在阅读 SQLAlchemy 文档,但我不理解它们。错误 (UnmappedInstanceError) 表示某些内容未映射。什么没有映射?我真的不懂 sqlalchemy,我想回去使用 naked sqlite,但是很多人推荐这个,所以我认为我应该学习它。这是回溯:

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)

File "C:\Users\Me\repos\mandj2\app\views.py", line 170, in add_manentry
db.session.add(q)

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\sqlalchemy\orm\scoping.py", line 149, in do
return getattr(self.registry(), name)(*args, **kwargs)

File "C:\Users\Me\repos\mandj\venv\lib\site-packages\sqlalchemy\orm\session.py", line 1452, in add
raise exc.UnmappedInstanceError(instance)

UnmappedInstanceError: Class '__builtin__.unicode' is not mapped

这里是适用的代码:

@app.route('/addm', methods=['POST'])
def add_mentry():
if not session.get('logged_in'):
abort(401)
form = MForm(request.form)
filename = ""
if request.method == 'POST':
cover = request.files['cover']
if cover and allowed_file(cover.filename):
filename = secure_filename(cover.filename)
cover = cover.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

q = request.form['name']
# do for 12 more fields
db.session.add(q)
db.session.commit()
flash('New entry was successfully posted')
return redirect(url_for('moutput'))

最佳答案

当您将非模型对象添加到 session 中时,您将得到 UnmappedInstanceError

在您的例子中,q 可能是一个 unicode 字符串,而不是模型对象。 (看来您使用的是 Python 2.x,因为在 Python 3.x 中,它会说 str),这是 UnmappedInstanceError: Class '__builtin__.unicode' is not mapped 的根本原因行>:

  File "C:\Users\Me\repos\mandj2\app\views.py", line 170, in add_manentry
db.session.add(q)

关于python - flask 中的 sqlalchemy.orm.exc.UnmappedInstanceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21196824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com