如何判断 SQLAlchemy 中发生回滚的原因?
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当异常发生时,你总是可以打印出错误:
You can always print out the error when exception occurs: