烧瓶中的sqlalchemy.orm.exc.UnmappedInstanceError [英] sqlalchemy.orm.exc.UnmappedInstanceError in flask

查看:568
本文介绍了烧瓶中的sqlalchemy.orm.exc.UnmappedInstanceError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been reading the SQLAlchemy docs, but I don't understand them. The error (UnmappedInstanceError) says something isn't mapped. What isn't mapped? I really don't get sqlalchemy and I want to go back to using naked sqlite, but so many people recommend this, so I thought I should learn it. Here is the traceback:

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'))

推荐答案

q = request.form['name']
# do for 12 more fields
db.session.add(q)

request.form['name']将返回unicode值.那你就...

request.form['name'] will return a unicode value. Then, you do...

db.session.add(q)

该会话的目标是跟踪实体(Python对象),而不是您似乎试图这样做的单个unicode值(请参见映射"部分 > ORM教程),但实际上您正在传递一个简单的unicode值

The goal of the session is to keep track of Entities (Python objects), not individual unicode values as you seem to be trying to do it (See here for more on what the session does). Thus, you should be adding objects that have a mapping (such as a User object as shown in the "Mapping" section of the ORM tutorial), but you're actually passing a simple unicode value

您使用的只是SQLAlchemy的一部分:ORM(对象关系映射器). ORM会尝试做一些事情,例如允许您创建一个新的python对象,并通过将对象添加"到会话中来自动生成SQL.

What you're using is just one part of SQLAlchemy: the ORM (Object-Relational Mapper). The ORM will try to do things like allow you to create a new python object and have the SQL automatically generaeted by "adding" the object to the session..

a = MyEntity()
session.add(a)
session.commit() # Generates SQL to do an insert for the table that MyEntity is for

请记住,您可以在不使用ORM功能的情况下使用SQLAlchemy.您可以执行db.execute('INSERT...', val1, val2)来替换您已经裸"的SQL. SQLAlchemy将为您提供连接池等(尽管如果您使用的是SQLite,则可能不关心连接池).

Keep in mind that you can use SQLAlchemy without using the ORM functionality. You could just do db.execute('INSERT...', val1, val2) to replace your already "naked" SQL. SQLAlchemy will provide you connection pooling, etc (although if you're using SQLite, you probably don't care about connection pooling).

如果您想了解Flask-SQLAlchemy,我首先建议您通过使用简单的脚本(如

If you want to understand Flask-SQLAlchemy, I would first recommend understanding how SQLAlchemy works (especially the ORM side) by trying it out using simple scripts (as shown in the tutorials. Then you'll understand how Flask-SQLAlchemy would work with it.

这篇关于烧瓶中的sqlalchemy.orm.exc.UnmappedInstanceError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆