On Tue, Sep 24, 2013 at 5:40 PM, Nicholas Long <nick.studioc...@gmail.com>wrote:

My mind keeps going back to events but of course there's the limitation to
> modifying Session state while handling various events. (
> http://docs.sqlalchemy.org/en/rel_0_7/orm/events.html#sqlalchemy.orm.events.MapperEvents.after_update
> )
>
> But what about using the SQL Expression API?
>
> *Relevant code snippet:*
>
> class MyModel(Base):
>     __tablename__ = "MyTable"
>     priid = Column(Integer(), primary_key=True)
>     secid = Column(Integer())
>
>
> @listens_for(MyModel, 'after_insert')
> def mymodel_after_insert(mapper, connection, target):
>     mytable = MyModel.__table__
>     priid = target.priid
>     statement = (mytable.update()
>                             .where(mytable.c.priid == priid)
>                             .values(secid=priid))
>     connection.execute(statement)
>
>
> Full code for reference:
> http://paste.pound-python.org/show/WVciGm4jCxgvz84jKrZy/
> SQLA echo: http://paste.pound-python.org/show/hSyCGisr0X5eupVdyk3f/
>
> The echo looks sane, though I can't be sure how the SQLAchemy internals
> are affected by this.
>
> Thank you for your input, Michael.
>

While I'm not using the 'two-step' approach this time, there will be future
column renames that are far more insidious and may require it, so I would
also like to know if the above will work as well. :)

-- 
- Ken Lareau

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to