On Jul 3, 2013, at 5:52 PM, Josh Kuhn <habi...@gmail.com> wrote:

> 
> I still get an integrity error because the after_flush happens after I've 
> already tried to insert the null gadgets. If I move the flush after the 
> widget is added to the session, but before the gadgets are, then the 
> query(Gadget).with_parent(widget) obviously won't find anything.

oh, well nullable=False.  then you obviously can't rely on UPDATE here, you 
need to get at those rows before they are inserted and after the Widget has 
machine_id, so you need to use flush events to get at individual cases:


1. when gadget is inserted, needs machine_id (add "widget" backref):

@event.listens_for(Gadget, "before_insert")
def before_gadget(mapper, connection, target):
    target.machine_id = target.widget.machine_id


2. when widget is updated, gadgets need new machine_id, here UPDATE is probably 
best since the Gadget might not be part of the flush:

@event.listens_for(Widget, "after_update")
def after_widget(mapper, connection, target):
    connection.execute(
        Gadget.__table__.update().
            values(machine_id=target.machine_id).
            where(Gadget.widget_id == target.widget_id)
    )







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