On Apr 26, 2013, at 4:01 PM, Daniel Grace <thisgenericn...@gmail.com> wrote:

> I'm still fairly new at sqlalchemy, and am still occasionally being surprised 
> by how sometimes-too-clever it is.  I ran into one of those moments today.
> 
> I have something that looks like this.  (All the tables are reflected.)
> 
> class Parent(Model, SimpleLookupTable):
>     __table__ = Table('parent')
>     # ....
> 
> class Child(Model):
>     __table__ = Table('child')
>     parent = relationship("Parent", lazy='joined', 
> backref=backref('children', lazy='lazy'))
>     # ....
> 
> 
> As part of my program design, I'm sometimes creating a partially-populated 
> Child() that serves as a 'template' for a child that might be created later, 
> but isn't at this point.
> 
> template = Child(parent = parent)
> do_lots_of_stuff()
> 
> I was a bit surprised to find that at this point, parent.children already 
> contains the new Child().  This makes sense when thinking about it under 
> normal circumstances... but in this particular case, I don't want this to be 
> part of the collection until it's actually added to the session (which I can 
> confirm it's not, I've tried both expunging and make_transient().
> 
> What's the best way to accomplish this?  The documented behavior of 
> cascade_backrefs=False is almost, but not quite, what I need.  

well backrefs do what they do, regarding synchronizing both sides of the 
collection/scalar, all the time.  There's no way to "turn it off" for certain 
object states.  So if you really need Child(parent) but not the collection, 
you'd need to associate "parent" on Child using some alternate attribute, like 
temporary thing "Child(pending_parent=parent)".   That or, don't use backrefs 
between Child.parent and Parent.children, that is also an option, though you'd 
need to be more cautious about mutating both sides before a flush.

-- 
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 http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to