On Wed, Feb 16, 2011 at 9:22 AM, Landreville <ja...@deadtreepages.com> wrote:
> Hi,
> I am trying to set a session variable (call a function with a value)
> in postgres at the beginning of every session that SQLAlchemy starts.
> I'm using the session variable to specify the currently logged in user
> for an auditing system (just a trigger on a couple tables). I have
> tried the before_commit ORM event, but it will set the variable after
> the other commands so the trigger does not pick it up. I also tried
> the before_flush event, but it seems to have a simlar problem -- it
> doesn't always set the variable at the beginning of the session.
>
> Is there a way to run a command at the beginning of every session,
> before anything else is run?

I'm going to hazard a response.
I needed to do the same thing, so I subclassed ConnectionProxy and
overrode the begin method.  Then when creating my engine I did this:

engine = create_engine(..., proxy=MyConnectionProxySubclass())

> Also, in these cases should I be using Session.execute or
> Session.connection.execute()?

I would also like an answer to that -- I typically use session.execute
whenever possible, except when reflecting tables (pretty much anything
that takes a 'bind' argument) I have to use session.connection().

-- 
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to