the _clone() on query is a shallow copy, otherwise it would be a serious performance hit for a query that takes several generative steps to build up. So you'd have to assign query's _order_by to a copy of itself if you want to modify it in place like that. you can probably use visitors.cloned_traverse() on the element to copy it, then also copy the list of elements as well (look at the source to query.order_by() to see the idea).
empty wrote: > > I realize I'm doing something hacky, but I'm wondering if this is the > expected behavior. The issue is that if I modify the modifier on a > UnaryExpression, even if I clone the Query ahead of time it modifies > other Query instances. See the following: > > import datetime > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy import * > from sqlalchemy.sql import operators > from sqlalchemy.orm import sessionmaker > > engine = create_engine('sqlite://') > Session = sessionmaker(bind=engine, autoflush=True, autocommit=False) > session = Session() > Base = declarative_base(engine=engine) > > class Category(Base): > __tablename__ = 'foo_category' > > id = Column('id', Integer, primary_key=True) > name = Column('name', String(50)) > created_at = Column('created_at', DateTime, nullable=True, > onupdate=datetime.datetime.now) > > def __repr__(self): > return self.name > > > Base.metadata.create_all() > > session.add_all([Category(name='zzeek'), > Category(name='jek'), > Category(name='empty')]) > > # order ascending > c1 = session.query(Category).order_by(asc(Category.name)) > assert [u'empty', u'jek', u'zzeek'] == [c.name for c in c1] > > # clone the query and verify still asc > c2 = c1._clone() > assert [u'empty', u'jek', u'zzeek'] == [c.name for c in c2] > > # reverse the order_by modifier on c1 > c1._order_by[0].modifier = operators.desc_op > assert [u'zzeek', u'jek', u'empty'] == [c.name for c in c1] > > # here's the problem, c2 is now modified > assert [u'zzeek', u'jek', u'empty'] == [c.name for c in c2] > > > Michael Trier > blog.michaeltrier.com > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---