On Wed, Sep 26, 2018 at 9:26 AM Mike Bayer <mike...@zzzcomputing.com> wrote:
>
> On Tue, Sep 25, 2018 at 9:09 PM Doug Miller <dpmp...@gmail.com> wrote:
> >
> > I think my issue is that before the commit/rollback
> > Alice.id is None but after Alice.id is set to some integer. I understand 
> > the object is transient but I wish that the primary key field was not 
> > modified in this way because if I want to try to reinsert Alice in a new 
> > transaction I have to write logic to introspect into the schema and unset 
> > the primary key. This behavior also breaks my mental model of “the object 
> > is the way it was before the commit failure and rollback”.
>
> ideally you'd be making a *new* Alice object altogether.   retry logic
> for failed transactions is usually coarse grained.   the unit of work
> process does not keep track of which attributes on each object were DB
> generated and which weren't and this would be extremely complicated /
> performance-impactful to implement.

note that a transient object can't be "expired" in any case as it is
assumed the object is part of a session context, and a "refresh from
the DB" trigger is added to it which fails if you aren't actually
attached to a Session.

So in this case you want to just set the attribute to None. Here's an
event handler that does that directly:

@event.listens_for(Session, "pending_to_transient")
def intercept_pending_to_transient(session, object_):
    pk_attrs = [
        attr.key for attr in
        inspect(object_).mapper.column_attrs if attr.expression.primary_key
    ]
    for attr in pk_attrs:
        delattr(object_, attr)


your test case then prints:

User(name='alice', id=None) User(name='bob', id=None)

now as far as how to distinguish which PK attributes you actually want
to set to None and such, you'd need to add additional logic to detect
the conditions you are looking for.   The event can be set on just an
individual Session object as needed, if that helps.



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

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