this is in the documentation for Query.delete(). One complexity of this operation, as opposed to issuing a delete() on the mapped Table directly, is that Query must keep the state of the parent Session synchronized with the SQL which is emitted. To accomplish this, the delete() (and update()) operations provide two schemes of figuring out what was actually affected so that the Session may be updated. One is to select the criterion given first so that the results can be matched against what's currently in the session. Another is to evaulate the current criterion in Python only against the Session's current contents. A caveat with this is that more complex criterion aren't supported, and the database's collation behavior (how to compare different character sets, casing conventions) is not honored. The third option is to disable it entirely. In the case of a delete(), despite the warning in the current documentation this option is pretty safe for deletes.
So try query()...delete(synchronize_session=False) On Jan 8, 2:21 am, Tomasz Narloch <toma...@wp.pl> wrote: > Piotr Kęplicz pisze:> Joe Riopel, środa 07 stycznia 2009 17:45: > > >>> Session.query(Person).filter_by(...).delete() > > >> Isn't that code still doing the select first, to get the object, and > >> then deleting it? > > > No. It's a Query object turned into a DELETE statement, just like first(), > > all() or one() would turn it into a SELECT statement. > > pylons 0.9.7, sqlalchemy 0.5 > This is copy paste from console: > > Pylons Interactive Shell > Python 2.5 (release25-maint, Jul 20 2008, 20:47:25) > [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] > > All objects from pyupo.lib.base are available > Additional Objects: > mapper - Routes mapper object > wsgiapp - This project's WSGI App instance > app - paste.fixture wrapped around wsgiapp > > >>> from pyupo.model.meta import Session > >>> from pyupo.model.emailbag import EmailBag as e > >>> s = Session.query(e).filter(e.order_id == 1).filter(e.active == > True).filter(e.dispatched == False) > >>> s.delete() > 07:10:35,545 INFO [sqlalchemy.engine.base.Engine.0x...6acL] BEGIN > 07:10:35,546 INFO [sqlalchemy.engine.base.Engine.0x...6acL] SELECT em > ailbag.id AS emailbag_id > FROM emailbag > WHERE emailbag.order_id = %s AND emailbag.active = %s AND emailbag.dis > patched = %s > 07:10:35,546 INFO [sqlalchemy.engine.base.Engine.0x...6acL] [1, 1, 0] > 07:10:35,547 DEBUG [sqlalchemy.engine.base.Engine.0x...6acL] Col > ('emailbag_id',) > 07:10:35,547 INFO [sqlalchemy.engine.base.Engine.0x...6acL] DELETE FROM > emailbag WHERE emailbag.order_id = %s AND emailbag.active = %s AND > emailbag.dispatched = %s > 07:10:35,547 INFO [sqlalchemy.engine.base.Engine.0x...6acL] [1, 1, 0] > 0L > >>> s.update({'active': False}) > 07:12:15,965 INFO [sqlalchemy.engine.base.Engine.0x...6acL] SELECT > emailbag.id AS emailbag_id FROM emailbag > WHERE emailbag.order_id = %s AND emailbag.active = %s AND > emailbag.dispatched = %s > 07:12:15,965 INFO [sqlalchemy.engine.base.Engine.0x...6acL] [1, 1, 0] > 07:12:15,965 DEBUG [sqlalchemy.engine.base.Engine.0x...6acL] Col > ('emailbag_id',) > 07:12:15,966 INFO [sqlalchemy.engine.base.Engine.0x...6acL] UPDATE > emailbag SET active=%s, updated_at=CURRENT_DATE WHERE emailbag.order_id > = %s AND emailbag.active = %s AND emailbag.dispatched = %s > 07:12:15,966 INFO [sqlalchemy.engine.base.Engine.0x...6acL] [0, 1, 1, 0] > 0L > > This examples say something else. > What is wrong with this query? > > Best Regards, > Tomek --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~----------~----~----~----~------~----~------~--~---