Could someone please give an example of this paragraph in the Documentation -- Using the session?

24 views
Skip to first unread message

Bao Niu

unread,
Mar 15, 2014, 5:26:40 AM3/15/14
to sqlal...@googlegroups.com
I've read this paragraph (http://docs.sqlalchemy.org/en/latest/orm/session.html#unitofwork-cascades) many many times and still can't think of a practical example of what is being discussed.

save-update cascade also cascades the pending history of the target attribute, meaning that objects which were removed from a scalar or collection attribute whose changes have not yet been flushed are also placed into the target session. This is because they may have foreign key attributes present which will need to be updated to no longer refer to the parent.

I don't think my English is the main stumbling block here because I understand the meaning of each word, but as soon as I'm putting them together I'm completely lost. Could someone give a simple example here to illustrate the main point in this paragraph please? Highly appreciated. Thanks.

Michael Bayer

unread,
Mar 15, 2014, 11:31:43 AM3/15/14
to sqlal...@googlegroups.com
you have every reason to be confused by that paragraph, which is using way too much terminology to express what’s important there.   at some point, we had to add a behavior which I thought would be confusing to people, so that paragraph tries badly to explain what it is.  I should replace it with just a simple sentence and an example.  Here’s the example:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    bs = relationship("B")

class B(Base):
    __tablename__ = 'b'

    id = Column(Integer, primary_key=True)
    a_id = Column(Integer, ForeignKey('a.id'))

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

sess = Session(e)
a1 = A()
b1 = B()

a1.bs = [b1]

sess.add(a1)
sess.commit()

a1.bs  # refresh a1.bs
sess.close()  # close out - sess is no longer associated with a1, b1

# all new session
sess2 = Session(e)

a1.bs.remove(b1)

sess2.add(a1)

# b1 was removed from a1.bs, but
# is in sess2 anyway! surprising!
assert b1 in sess2

# because we need it for the flush, it's still here:
from sqlalchemy import inspect
print inspect(a1).attrs.bs.history.deleted






--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Gery .

unread,
Mar 15, 2014, 12:07:46 PM3/15/14
to sqlal...@googlegroups.com, sqlal...@googlegroups.com
I also appreciate such example! Hope simple terminology with a simple example can replace the sometimes complicated documentation.

Sent from my i386

Michael Bayer

unread,
Mar 15, 2014, 4:10:42 PM3/15/14
to sqlal...@googlegroups.com
well thats my contribution for today, this is mostly rewritten: http://sqlalchemy.readthedocs.org/en/rel_0_9/orm/session.html#cascades

Bao Niu

unread,
Mar 15, 2014, 7:41:02 PM3/15/14
to sqlal...@googlegroups.com

Thanks a lot Michael! Just a trivial question here, I noticed in your first reply you used:
 # refresh a1.bs

Why do we need to refresh it? I tried it in my terminal and it doesn't emit any sql. Is this one if those secret techniques that differentiate a sqlalchemy ninja and a newbie?;)

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/MSiBcFB3cFI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.

Michael Bayer

unread,
Mar 16, 2014, 1:58:24 PM3/16/14
to sqlal...@googlegroups.com
On Mar 15, 2014, at 7:41 PM, Bao Niu <niub...@gmail.com> wrote:

Thanks a lot Michael! Just a trivial question here, I noticed in your first reply you used:
 # refresh a1.bs

Why do we need to refresh it? I tried it in my terminal and it doesn't emit any sql. Is this one if those secret techniques that differentiate a sqlalchemy ninja and a newbie?;)



it should emit SQL, because the sess.commit() call above has expired the collection.  in my script it fails down on “a1.bs.remove()” if i don’t call that first.

Bao Niu

unread,
Mar 16, 2014, 10:45:21 PM3/16/14
to sqlal...@googlegroups.com
Hi Michael,

It really works like magic! I mean the refreshing part. What exactly happens when refreshing a1.bs? Does it actually replace the old b1 inside the collection with a newer one? I can't wrap my mind around it. And more importantly, does it mean I should *refresh* every attributes before close a session, in order to carry their attribute over to the next session? 
What is the reason not making such *refreshing* automatic by default???

Sorry for bringing up another question here.

Michael Bayer

unread,
Mar 16, 2014, 11:24:27 PM3/16/14
to sqlal...@googlegroups.com

a1.bs only loads something if there’s nothing loaded already, or if the value was “expired”.   

usually, as long as you keep objects associated with a Session, there’s no need to worry about it as things load as they are needed.  but in that specific example, I’m moving an object from one session to another and trying to trip a condition that only shows up if the object were modified while it’s “detached”.   it isn’t a usual thing.



On Mar 16, 2014, at 10:45 PM, Bao Niu <niub...@gmail.com> wrote:

Hi Michael,

It really works like magic! I mean the refreshing part. What exactly happens when refreshinga1.bs? Does it actually replace the old b1 inside the collection with a newer one? I can't wrap my mind around it. And more importantly, does it mean I should *refresh* every attributes before close a session, in order to carry their attribute over to the next session? 

To unsubscribe from this group and all its topics, send an email tosqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email tosqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.

To unsubscribe from this group and all its topics, send an email tosqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email tosqlalchemy+...@googlegroups.com.

Bao Niu

unread,
Mar 16, 2014, 11:58:53 PM3/16/14
to sqlal...@googlegroups.com
Got it! It works, that's the main thing!



To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages