On Feb 16, 2011, at 12:54 PM, Jon Nelson wrote:

> On Wed, Feb 16, 2011 at 10:51 AM, Michael Bayer
> <mike...@zzzcomputing.com> wrote:
>> 
>> On Feb 16, 2011, at 10:22 AM, Landreville 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?
>> 
>> SessionExtension has an after_begin() hook for this purpose.
> 
> Is doing the way I suggested (as a ConnectionProxy subclass, via
> 'proxy' argument to create_engine) better or worse? Can you help me to
> understand why?

Well if my app used the engine or connection elsewhere, to create tables, etc., 
I might not want commands specific to the session to be firing off.    If the 
logic is session-specific then hanging it off of Session is a better place to 
put it.   

but if these are just some configurational commands, doesn't matter much.   I'd 
usually target PoolListener, if it was some connection-wide config.

> 
>>> Also, in these cases should I be using Session.execute or
>>> Session.connection.execute()?
>> 
>> Session.execute() is a shorthand version of Session.connection.execute().    
>> The former also coerces plain textual statements to 
>> sqlalchemy.sql.expression.text() so that a single string format may be 
>> passed.
> 
> Aha! Very nice. What about using instances of Session where 'bind'
> arguments are required?

Upgrade to 0.7 and 'bind' is an argument for both, if you have one lying 
around.    The only reason you'd the Session methods with "bind" is if you want 
your statements to be in the context of an ongoing transaction in the Session 
regarding that bind.  Otherwise just call bind.execute().

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