如何判断 SQLAlchemy 中发生回滚的原因?

发布于 2024-10-31 13:12:46 字数 1889 浏览 1 评论 0原文

当我使用 SQLAlchemy 插入 MySQL 时,我看到开发服务器的输出日志中出现消息“ROLLBACK”。我如何知道为什么会发生回滚?

2011-04-10 00:35:32,736 INFO  [sqlalchemy.engine.base.Engine.0x...4710][MainThread] INSERT INTO pageview (time, unit_id, visitor_id, url_id, referrer_id, reservation_id, visit_id) VALUES (%s, %s, %s, %s, %s, %s, %s)
2011-04-10 00:35:32,736 INFO  [sqlalchemy.engine.base.Engine.0x...4710][MainThread] (datetime.datetime(2011, 1, 31, 0, 1, 53), 120L, 5538L, 11075L, 11076L, 5538L, None) 
2011-04-10 00:35:32,737 INFO  [sqlalchemy.engine.base.Engine.0x...4710][MainThread] ROLLBACK 
Starting server in PID 10158. 
serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

我注意到字母“L”出现在每个外键值之后(例如:unit_id 字段是“120L”而不是“120”)。这可能与这个问题有关吗?

这是执行插入的 python 代码:

@classmethod
def unconverted(class_):
    session = DBSession()
    return session.query(class_).filter(class_.pageview == None).order_by(class_.time).limit(5).all()

@classmethod
def convert_all(class_):
    session = DBSession()

    unconverted = class_.unconverted()
    for item in unconverted:

        pageview = PageView(raw_request=item)
        item.pageview = pageview
        session.add(item)

    session.flush()
    transaction.commit()
    session.close()

编辑:

按照 plaes 的建议,在 session.flush() 上执行 try/ except 时出现以下异常:

2011-04-10 11:33:44,462 INFO  [sqlalchemy.engine.base.Engine.0x...3750][MainThread] ROLLBACK
(IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`metrics`.`pageview`, CONSTRAINT `pageview_ibfk_1` FOREIGN KEY (`unit_id`) REFERENCES `unit` (`id`))') 'INSERT INTO pageview (time, unit_id, visitor_id, url_id, referrer_id, reservation_id, visit_id) VALUES (%s, %s, %s, %s, %s, %s, %s)' (datetime.datetime(2011, 1, 31, 0, 1, 53), 120L, 5608L, 11215L, 11216L, 5608L, None)

发生了什么错误?

When I insert into MySQL with SQLAlchemy, I see the message "ROLLBACK" appear in the outputed log for the development server. How can I tell why the rollback is happening?

2011-04-10 00:35:32,736 INFO  [sqlalchemy.engine.base.Engine.0x...4710][MainThread] INSERT INTO pageview (time, unit_id, visitor_id, url_id, referrer_id, reservation_id, visit_id) VALUES (%s, %s, %s, %s, %s, %s, %s)
2011-04-10 00:35:32,736 INFO  [sqlalchemy.engine.base.Engine.0x...4710][MainThread] (datetime.datetime(2011, 1, 31, 0, 1, 53), 120L, 5538L, 11075L, 11076L, 5538L, None) 
2011-04-10 00:35:32,737 INFO  [sqlalchemy.engine.base.Engine.0x...4710][MainThread] ROLLBACK 
Starting server in PID 10158. 
serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

I am noticing that the letter "L" appears after every foreign key value (ex: the unit_id field is "120L" instead of "120"). Could that be related to this issue?

Here is the python code that is doing the insert:

@classmethod
def unconverted(class_):
    session = DBSession()
    return session.query(class_).filter(class_.pageview == None).order_by(class_.time).limit(5).all()

@classmethod
def convert_all(class_):
    session = DBSession()

    unconverted = class_.unconverted()
    for item in unconverted:

        pageview = PageView(raw_request=item)
        item.pageview = pageview
        session.add(item)

    session.flush()
    transaction.commit()
    session.close()

EDIT:

Following the advice of plaes, I get the following exception when doing a try/except on session.flush():

2011-04-10 11:33:44,462 INFO  [sqlalchemy.engine.base.Engine.0x...3750][MainThread] ROLLBACK
(IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`metrics`.`pageview`, CONSTRAINT `pageview_ibfk_1` FOREIGN KEY (`unit_id`) REFERENCES `unit` (`id`))') 'INSERT INTO pageview (time, unit_id, visitor_id, url_id, referrer_id, reservation_id, visit_id) VALUES (%s, %s, %s, %s, %s, %s, %s)' (datetime.datetime(2011, 1, 31, 0, 1, 53), 120L, 5608L, 11215L, 11216L, 5608L, None)

What is this error occurring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-11-07 13:12:46

当异常发生时,你总是可以打印出错误:

try:
    transaction.commit()
except Exception, e:
    session.rollback()
    print str(e)

You can always print out the error when exception occurs:

try:
    transaction.commit()
except Exception, e:
    session.rollback()
    print str(e)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文