<div dir="ltr">I thought I might answer my own question.<div><br></div><div>I haven't been able to get an elegant solution to this problem. I can get the before_insert and before_update events to do things, but the only way I can get a change in my Foo table to show end up with a new row in the Bar table is to use the connection object's execute method to run some SQL. I didn't like that. </div><div><br></div><div>The final problem I had with this method is that I need to write the Foo row's primary key into the Bar row. before_insert fires before the Foo row has a primary key.</div><div><br></div><div>What I did instead was a bit of a kludge, but in my new_foo and edit_foo view methods I call a static method Bar.saveFoo(foo_instance) which creates a new Bar row. I know I could do this with database triggers, but I don't want tie myself to a particular database</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 3, 2016 at 9:38 AM, Craig Amundsen <span dir="ltr"><<a href="mailto:amundsec@gene.com" target="_blank">amundsec@gene.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi -<div><br></div><div>I'm trying to use an after_insert event to do some bookkeeping in a Flask app and am having issues.</div><div><br></div><div>Before we get too far I've been basing my app on the Grinberg Flask Web Development book.</div><div><br></div><div>Simplifying wildly, lets say I have these two tables</div><div><br></div><div><font face="monospace, monospace">class Foo(db.Model)</font></div><div><font face="monospace, monospace">    __tablename__ = 'foos'</font></div><div><font face="monospace, monospace">    id = db.Column(db.Integer, primary_key = True)</font></div><div><font face="monospace, monospace">    col_one = db.Column(db.String(32))</font></div><div><font face="monospace, monospace">    col_two = db.Column(db.String(32))</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    @staticmethod</font></div><div><font face="monospace, monospace">    def on_change(mapper, connection, target):</font></div><div><font face="monospace, monospace">        # save a new row in table Bar</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class Bar(db.Model)</font></div><div><font face="monospace, monospace">    __tablename__ = 'bars'</font></div><div><font face="monospace, monospace">    id = db.Column(db.Integer, primary_key = True)</font></div><div><font face="monospace, monospace">    col_a = db.Column(db.Integer)</font></div><div><font face="monospace, monospace">    col_b = db.Column(db.String(16))</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">db.event.isent(Foo, 'after_insert', Foo.on_change)</font></div><div><br></div><div>I really only want to add the row to Bar if there is an actual write to Foo.</div><div><br></div><div>I note that if I make the listened-for event before_insert and in the on_change method I do</div><div><font face="monospace, monospace">b = Bar(col_a = 5, col_b = 'Hi Craig')</font></div><div><font face="monospace, monospace">db.session.add(b)</font></div><div>I get shouty messages in the console, but it works.</div><div><br></div><div>I see various things when I google for using the connection object in an after_insert event, but nothing that is actually a workable example that I can transmogrify for my actual use. </div><div><br></div><div>I could put something in the new_foo method in views.py that writes a Bar entry, but I'd like to use the event.</div><div><br></div><div>Could someone point me in the proper direction for getting a connection.execute call to work?</div><div><br></div><div>Thanks,</div><div>- Craig</div></div>
</blockquote></div><br></div>