[Flask] after_insert question

Craig Amundsen amundsen.craig at gene.com
Fri Jun 3 12:38:51 EDT 2016

Hi -

I'm trying to use an after_insert event to do some bookkeeping in a Flask
app and am having issues.

Before we get too far I've been basing my app on the Grinberg Flask Web
Development book.

Simplifying wildly, lets say I have these two tables

class Foo(db.Model)
    __tablename__ = 'foos'
    id = db.Column(db.Integer, primary_key = True)
    col_one = db.Column(db.String(32))
    col_two = db.Column(db.String(32))

    def on_change(mapper, connection, target):
        # save a new row in table Bar

class Bar(db.Model)
    __tablename__ = 'bars'
    id = db.Column(db.Integer, primary_key = True)
    col_a = db.Column(db.Integer)
    col_b = db.Column(db.String(16))

db.event.isent(Foo, 'after_insert', Foo.on_change)

I really only want to add the row to Bar if there is an actual write to Foo.

I note that if I make the listened-for event before_insert and in the
on_change method I do
b = Bar(col_a = 5, col_b = 'Hi Craig')
I get shouty messages in the console, but it works.

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.

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.

Could someone point me in the proper direction for getting a
connection.execute call to work?

- Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160603/26d5f767/attachment.html>

More information about the Flask mailing list