On 01/27/2017 11:23 AM, Philip Scott wrote:
I think it's easier to explain in an example than for me to waffle on
trying to explain myself :)

that's always the best :) because i find it easier to run a program and see what it does rather than read it :)



s.expire_all()

b3.a = a1
assert(b3 in a1.bs) # Yep, as you might expect

b2.a = a1
assert(b2 in a1.bs) # Nope

OK the lack of autoflush here and the sudden expire_all() is leading to some subtle states here:

# 1. b2 is added to a1.bs, is now present in the session as
# "pending" e.g. not inserted yet
a1.bs.append(b2)

# 2. we suddenly expire everything, including a1.bs.  b2 is no longer
# in a1.bs.  it is also not inserted yet. *but*, it's pending still;
# "b2 in s.new" will show this.  So, as a bonus, because b2 is not
# "persistent", it isn't expired either.   "b2.a is a" will show True.
# this is the reason for the thing down below.   also b3 is still
# pending too.
s.expire_all()

# 3. b3.a set up, the backref fires off but because
# a1.bs is unloaded, it places it into a "pending" collection
# that will be populated when a1.bs is actually loaded
b3.a = a1

# 4. hitting a1.bs loads it, adds b3 in as pending.  is not flushed,
# but we see in the pending collection
assert(b3 in a1.bs)

# 5. b2.a set up, the backref fires off.  it has a rule, if we are
# replacing the currently existing reference with the same object, do
# nothing.  this is to prevent lots of obvious recursion errors and
# such.  so!  nothing happens.  you don't see b2 in a1.bs until you
# flush.
b2.a = a1

# doing this allows you to see it
# s.flush()

assert(b2 in a1.bs) # Nope



--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to