SQLAlchemy 不会更新我的数据库 [英] SQLAlchemy won't update my database

查看:112
本文介绍了SQLAlchemy 不会更新我的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQLAlchemy-0.7.8 制作 Pyramid 应用程序.我使用的是 64 位 Python3.2.

I'm making a Pyramid app using SQLAlchemy-0.7.8. I'm using 64bit Python3.2.

问题是,为什么下面的函数没有向数据库提交任何东西?

The question is, why does the following function not commit anything to the database?

def create_card(sText,sCard):
    """
    create a wildcard instance if all is well (ie,sCard match in sText)
    return 
        oCard, dCard
    otherwise return False,False
    """
    oMatch = re.search(sCard,sText)
    if oMatch:
        oCard = WildCard()
        #set up some stuff about the WildCard

        DBSession.add(oCard)
        DBSession.flush()
        dCard = {
                    'id'            : oCard.id,
                    'span'          : oMatch.span(),
                    'card'          : oCard.card_string,
                            }
        return oCard,dCard
    return False,False  

我从另一个脚本导入 DBSession.它的定义如下:

I import DBSession form another script. it is defined as follows:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

以下是一些背景信息:

我正在制作的应用程序将用于通过使用正则表达式来表征大型 HTML 块.如果应用程序卡住并认为一段文本应该有一个通配符匹配,那么用户会得到一个小表格来填写.一旦提交了表格,create_card 就会被调用.如果通配符与字符串匹配,则创建一个通配符实例.

The app I'm making is to be used to characterize large blocks of HTML through use of regular expressions. If the app gets stuck and thinks there should be a wilcard match for a piece of text then the user is given a little form to fill in. Once the form is committed create_card is called. If the wildcard is matched against the string then a WildCard instance is created.

WildCard 类没什么特别的,它只是存储一个字符串和几个整数.如果我打印出 dCard,它看起来像 WildCard 已成功提交,因为它有一个整数 id.如果我不在数据库会话中调用刷新,则 dCard['id'] 为 None.

The WildCard class is nothing special, it just stores a string and a few integers. If I print out dCard it looks like the WildCard was sucessfully committed because it has an integer id. If I don't call flush on the database session then dCard['id'] is None.

id 字段看起来像:

id = Column(Integer,Sequence('wild_seq'), primary_key=True)

添加和刷新行导致以下控制台输出:

The add and flush lines cause the following console output:

2012-09-16 12:30:34,845 INFO  [sqlalchemy.engine.base.Engine][Dummy-2] INSERT INTO wildcard_wildcards (card_string, range_id, brand_id, category_id, group_cat_map_id, heading_group_id, heading_to_grp_map_id, heading_id, value_map_id, igneore_match) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2012-09-16 12:30:34,845 INFO  [sqlalchemy.engine.base.Engine][Dummy-2] ('sCard contents', None, None, None, None, None, None, None, None, 0)

所以到目前为止,一切都按预期进行.

So up until this point everything is behaving pretty as is expected.

问题来了:尽管 WildCard 实例看起来像是已提交到数据库,并且没有引发异常,但直接检查数据库表明没有进行任何更改.

Here's the problem: Even though the WildCard instance looks like it has been committed to the database, and no Exceptions are raised, direct examination of the database shows that no changes are made.

用 commit() 替换 flush() 会引发以下异常:

replacing flush() with commit() raises the following exception:

AssertionError: Transaction must be committed using the transaction manager

推荐答案

您需要提交您的交易.

您可以显式执行此操作(通过调用 DBSession.commit() 或使用 pyramid_tm 中间件;后者在成功响应时自动提交事务(使用 2xx HTTP 响应).

You can do this explicitly (by calling DBSession.commit() or by using the pyramid_tm middleware; the latter commits transactions automatically on successful responses (with a 2xx HTTP response).

如果您将 ZopeTransactionExtension 扩展与会话生成器一起使用,后者只会为 SQLAlchemy 提交事务:

The latter only commits transactions for SQLAlchemy if you use the ZopeTransactionExtension extension with your session maker:

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

如果您已经在使用 ZopeTransactionExtension 并且想要明确提交您的交易,您需要使用 transaction 包:进口交易

If you are already using the ZopeTransactionExtension and want to explicitly commit your transactions, you need to use the transaction package: import transaction

transaction.commit()

这篇关于SQLAlchemy 不会更新我的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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