On Aug 31, 10:33 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> It sounds like you're looking for INSERT from SELECT.   SQL certainly allows 
> this and you can build SQLAlchemy expression constructs that are similar, 
> there's a mini example 
> athttp://www.sqlalchemy.org/docs/reference/ext/compiler.html?highlight=....   
> In this case:
>
>         INSERT into email_addresses (user_id, email_address) (
>         SELECT id, 'we...@foo.com' from users where name='wendy')
>
> INSERT from SELECT is useful when you're writing database migrations - a new 
> set of tables is to receive the records from the old tables en masse.    For 
> very large databases, while it might take days to fetch all the rows into a 
> client application and re-INSERT them outwards, a migration script would use 
> INSERT from SELECT so that the operation takes place within the database 
> process space and be much more efficient.
>
> INSERT from SELECT is a bulk operation so is not really something an ORM 
> concerns itself with.

Thanks for clarifying and for the example... it explains why I
couldn't figure out how to do it with the ORM.

So if you were creating a simple "AddUserEmail" function you would do
it as in my two-query example below?

AddUserEmail(UserName, NewEmail):
    sess = Session()
    newAddress = Address(email_address = NewEmail)
    user = sess.query(User).filter_by(name = UserName).one()
    user.addresses.append(newAddress)
    sess.commit()

I'm content with that if that is the "best" way.  It is just the
annoying and ever present optimization demon sitting on my shoulder
wanting me to cut that down to one query, but I can shush him (I
think).

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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