Sql Alchemy 重复提交

发布于 2024-11-16 21:53:06 字数 1623 浏览 4 评论 0原文

我目前在 Cherrypy 应用程序中遇到问题。 我自己的自定义会话模块,在执行 session.add() 时,完全相同的对象会更新两次。

cherrypy.request.SessionManager.user_data = user

try:

    db_session.add(cherrypy.request.SessionManager)
    db_session.commit()

会回来

2011-06-21 09:16:48,991 INFO sqlalchemy.engine.base.Engine.0x...04cL BEGIN (implicit)
2011-06-21 09:16:49,015 INFO sqlalchemy.engine.base.Engine.0x...04cL SELECT ..... 
FROM "Clients_Users" 
WHERE "Clients_Users".username = %(username_1)s AND "Clients_Users".password = %(password_1)s 
LIMIT 1 OFFSET 0
2011-06-21 09:16:49,015 INFO sqlalchemy.engine.base.Engine.0x...04cL {'password_1': '123', 'username_1': u'1'}
2011-06-21 09:16:49,047 INFO sqlalchemy.engine.base.Engine.0x...04cL UPDATE "SYS_Sessions" SET user_data=%(user_data)s WHERE "SYS_Sessions".id = %(SYS_Sessions_id)s
2011-06-21 09:16:49,067 INFO sqlalchemy.engine.base.Engine.0x...04cL {'SYS_Sessions_id': 92L, 'user_data': <psycopg2._psycopg.Binary object at 0x8a7c06c>}
2011-06-21 09:16:49,071 INFO sqlalchemy.engine.base.Engine.0x...04cL COMMIT
2011-06-21 09:16:49,093 INFO sqlalchemy.engine.base.Engine.0x...04cL BEGIN (implicit)
2011-06-21 09:16:49,095 INFO sqlalchemy.engine.base.Engine.0x...04cL UPDATE "SYS_Sessions" SET user_data=%(user_data)s WHERE "SYS_Sessions".id = %(SYS_Sessions_id)s
2011-06-21 09:16:49,095 INFO sqlalchemy.engine.base.Engine.0x...04cL {'SYS_Sessions_id': 92L, 'user_data': <psycopg2._psycopg.Binary object at 0x8a8424c>}
2011-06-21 09:16:49,108 INFO sqlalchemy.engine.base.Engine.0x...04cL COMMIT

有人见过这个吗?

PS 这在我制作的其余模块中不会发生。

I'm currently facing a problem in my Cherrypy application.
Im my own custom session module, when performing session.add(), the exact same object gets updated twice.

cherrypy.request.SessionManager.user_data = user

try:

    db_session.add(cherrypy.request.SessionManager)
    db_session.commit()

Will Return

2011-06-21 09:16:48,991 INFO sqlalchemy.engine.base.Engine.0x...04cL BEGIN (implicit)
2011-06-21 09:16:49,015 INFO sqlalchemy.engine.base.Engine.0x...04cL SELECT ..... 
FROM "Clients_Users" 
WHERE "Clients_Users".username = %(username_1)s AND "Clients_Users".password = %(password_1)s 
LIMIT 1 OFFSET 0
2011-06-21 09:16:49,015 INFO sqlalchemy.engine.base.Engine.0x...04cL {'password_1': '123', 'username_1': u'1'}
2011-06-21 09:16:49,047 INFO sqlalchemy.engine.base.Engine.0x...04cL UPDATE "SYS_Sessions" SET user_data=%(user_data)s WHERE "SYS_Sessions".id = %(SYS_Sessions_id)s
2011-06-21 09:16:49,067 INFO sqlalchemy.engine.base.Engine.0x...04cL {'SYS_Sessions_id': 92L, 'user_data': <psycopg2._psycopg.Binary object at 0x8a7c06c>}
2011-06-21 09:16:49,071 INFO sqlalchemy.engine.base.Engine.0x...04cL COMMIT
2011-06-21 09:16:49,093 INFO sqlalchemy.engine.base.Engine.0x...04cL BEGIN (implicit)
2011-06-21 09:16:49,095 INFO sqlalchemy.engine.base.Engine.0x...04cL UPDATE "SYS_Sessions" SET user_data=%(user_data)s WHERE "SYS_Sessions".id = %(SYS_Sessions_id)s
2011-06-21 09:16:49,095 INFO sqlalchemy.engine.base.Engine.0x...04cL {'SYS_Sessions_id': 92L, 'user_data': <psycopg2._psycopg.Binary object at 0x8a8424c>}
2011-06-21 09:16:49,108 INFO sqlalchemy.engine.base.Engine.0x...04cL COMMIT

Has anyone seen this before ?

P.S This doesn't happen in the rest of the modules I have made.

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

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

发布评论

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

评论(2

渔村楼浪 2024-11-23 21:53:06

您可以尝试将 user_data 添加到会话中,而不是将 SessionManager 对象本身添加到会话中。这至少会使调试更加简单。

事实上,我不确定你在cherrypy SessionManager中是否有一个数据库会话——这似乎是不可取的,但也许sqlalchemy可以处理它(?)或者可能正在对无法完成的对象进行多次更改在数据库中完全相同的时间 - 例如,也许这就是某些级联在 echo on(?) 下的样子。您使用的级联值是多少?

我不确定第二次读取 sql 时更新了什么。它看起来只是一些隐含的东西。您可能需要显示这些对象的一些代码或导致更新以获得更好答案的一些代码。但也许上面的直觉会有所帮助。祝你好运

You might try adding the user_data to the session rather than adding the SessionManager object itself to the session. This would at least make debugging more straightforward.

As it is, I'm not sure whether you have a database session inside the cherrypy SessionManager-- which would seem undesirable, but maybe sqlalchemy can handle it(?) Or perhaps are making multiple changes to the object that can't be done at exactly the same time in the database-- e.g. maybe this is how some cascades look with echo on(?). What value of cascade are you using?

I'm not sure what is being updated the second time from reading the sql. It looks like only something implicit. You probably need to show some of the code for these objects or some of the code that causes the update to get a better answer. But maybe the above intuition will be helpful. Good luck

2024-11-23 21:53:06

好的,抱歉这么晚了,但我当时正在度假。

无论如何,为了防止双重提交,您需要从会话中删除对象,并使用 session.expunge()

还有一件事。

使用 session.merge()
将允许您重用被删除的对象。

希望这可以帮助有需要的人。

PS:请记住,一个对象可能会行为不当,或者任何未来的炼金术补丁都可能会破坏兼容性。所以亵渎是正确的。

Ok , Sorry for such a great delay but i was on vacation.

Anyway in order prevent the double commit , you need to remove the object from the session and you do that by using session.expunge().

One More Thing .

Using session.merge()
Will allow you to reuse the object that was expunged,.

Hope this helps someone in need.

P.S : Keep into consideration that an object may misbehave or that any future patch to alchemy may break the compatibility.So profane is Right.

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