[sqlalchemy] expunge cascade behavior change

2012-01-31 Thread Kent Bower
Somewhere between 0.6.4 and 0.7.5, the expunge cascade behavior 
changed.  Can you help me understand what changed/point me to the 
ticket?  The attached script assertions succeed in 0.6.4 but the last 
one fails in 0.7.5.  It doesn't seem wrong, but I'm wondering what the 
behavior was defined as previously and if that was considered a bug, etc.


Thanks,
Kent

--
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.orm.util import has_identity

engine = create_engine('sqlite:///', echo=True)
metadata = MetaData(engine)
Session = sessionmaker(bind=engine)

rocks_table = Table(rocks, metadata,
Column(id, Integer, primary_key=True),
)

bugs_table = Table(bugs, metadata,
Column(id, Integer, primary_key=True),
Column(rockid, Integer, ForeignKey('rocks.id'),),
)

class Rock(object):
def __repr__(self):
return 'Rock@%d: id=[%s] in session:[%s] has_identity[%s]' % (id(self), self.__dict__.get('id'), self in session, has_identity(self))

class Bug(object):
def __repr__(self):
return 'Bug@%d: id=[%s] rockid[%s] with rock[%s]' % (id(self), self.__dict__.get('id'), self.__dict__.get('rockid'), self.__dict__.get('rock','not set'))


mapper(Rock, rocks_table,
properties={'bugs': relationship(Bug,
cascade='all,delete-orphan', 
backref=backref('rock',cascade='refresh-expire,expunge'))
})

mapper(Bug, bugs_table)


metadata.create_all()

session = Session()


# add a rock and bug
rock=Rock()
rock.id = 0
bug=Bug()
bug.id = 0
rock.bugs.append(bug)
session.add(rock)

session.commit()

# later... new session
session = Session()
rock = session.query(Rock).get(0)
rock.bugs.append(Bug())

assert rock in session

rock.bugs = []

assert rock in session


[sqlalchemy] Expunge

2010-09-25 Thread Mark Erbaugh
If I retrieve data strictly for reporting or other read-only use, e.g. the 
session will not be used to update data, should I expunge the objects returned 
by the query from the session?  If I just let the session object go out of 
scope is that sufficient?

Thanks,
Mark

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Expunge

2010-09-25 Thread Peter Hansen

On 2010-09-25 2:29 PM, Mark Erbaugh wrote:

If I retrieve data strictly for reporting or other read-only use,
e.g. the session will not be used to update data, should I expunge
the objects returned by the query from the session?  If I just let
the session object go out of scope is that sufficient?


If you're not modifying the objects, then you don't have to do anything 
at all.  If you are modifying them, then as long as you don't call 
commit() on the session, the changes will be discarded when the session 
is deleted.  (That's assuming you have autocommit==False).


(Also, I think go out of scope is ambiguous in Python, where what 
really matters is whether there are other references to the session. 
Only when the last reference is removed is the session garbage-collected 
and the changes will discarded/rolled-back.  Having a local variable 
referencing the session go out of scope does nothing if there are 
other non-local references to the same session.  But you probably know 
all that. :) )


--
Peter Hansen

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] expunge vs clear

2008-02-02 Thread Jonathon Anderson

I'm using an orm configuration with a sessionmaker().mapper doing the
mapping between my classes and my metadata.

The documentation says that doing a Session.clear() should do the
equivalent of a Session.expunge(inst) for all instances attached to
the session.

I have one instance attached to the session, and do a Session.clear();
but if I then re-query to get that instance, I get

InvalidRequestError: Could not update instance '[EMAIL PROTECTED]',
identity key (class 'cobalt.model.base.Job', ('localhost', 0),
None); a different instance with the same identity key already exists
in this session.

If I replace the Session.close() with a Session.expunge(inst), the
error goes away.

Any ideas?

~jon


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---