On Aug 31, 12:16 pm, Michael Bayer <[email protected]> wrote:
> However I forgot one other approach that's also overkill for the general
> case, but is more useful with the ORM, which is that you can embed subqueries
> and other expressions into a flush. I usually use it with UPDATE statements
> when I need to do something like increment a counter atomically, which is the
> more important use case. But here you could use it with your INSERT:
>
> from sqlalchemy import select
>
> session.add(
> Address(
>
> user_id=select([User.id]).where(User.name=='wendy').as_scalar(),
> email_address='[email protected]'
> )
> )
>
Thanks!! That works perfectly with one query... my optimization demon
is appeased. It also opens my eyes to some more interesting
SqlAlchemy options for me. I appreciate it.
> Although you should probably look into having a longer spanning transaction
> than just within one function like that.
It was just a sample, although my sqla session lifetimes often aren't
much longer than that. I'm mostly using scoped sessions for handling
remote API calls. I've tinkered with trying to carry sqla sessions
between rpc calls (tied by login sessions in my system) to avoid so
many connect/reconnect cycles, but it got extremely sketchy and I
realized I couldn't guarantee anything... so it is back to an sqla
session per RPC instance.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.