[sqlalchemy] Automatically setting fields on collection append

2013-07-03 Thread Josh Kuhn
I'm trying to get a certain access pattern to work and I need a bit of help: https://gist.github.com/deontologician/5922496 What I'm trying to do is use an association proxy to create a view of a collection that looks like a list of dictionaries (for serializing to json). I also want to update

Re: [sqlalchemy] Automatically setting fields on collection append

2013-07-03 Thread Michael Bayer
On Jul 3, 2013, at 4:33 PM, Josh Kuhn deontologic...@gmail.com wrote: I'm trying to get a certain access pattern to work and I need a bit of help: https://gist.github.com/deontologician/5922496 What I'm trying to do is use an association proxy to create a view of a collection that looks

Re: [sqlalchemy] Automatically setting fields on collection append

2013-07-03 Thread Michael Bayer
On Jul 3, 2013, at 5:06 PM, Michael Bayer mike...@zzzcomputing.com wrote: Do the UPDATE through Session.execute() so it's within the same transaction. .. or even just query(Gadget).with_parent(some_widget).update({machine_id: some_widget.machine_id}), then you can have those Gadget objects

Re: [sqlalchemy] Automatically setting fields on collection append

2013-07-03 Thread Josh Kuhn
I've attempted the following: from sqlalchemy import Column, Integer, String, ForeignKey, create_engine from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.event import listen from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.associationproxy import

Re: [sqlalchemy] Automatically setting fields on collection append

2013-07-03 Thread Michael Bayer
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

Re: [sqlalchemy] Automatically setting fields on collection append

2013-07-03 Thread Josh Kuhn
On Wed, Jul 3, 2013 at 7:30 PM, Michael Bayer mike...@zzzcomputing.comwrote: @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