Hello.

I would like to convert the following

    # bulk insert of several rows
    new_rows = [{'foo': 1, 'bar': 2}, ...]
    cls.__table__.insert().execute(new_rows)

to session.execute(...) style.

I came up with

    ins = cls.__table__.insert()
    for each in new_rows:
        ins = ins.values(each)
    session.execute(ins)

but this only executes the last insert.

My assumption is that the bulk insert will be faster than several independent
one-row inserts.

I think I need to execute it via session because my entire transaction is as
follows:
* I create a new object in the session (using ORM layer).
* Flush the session so the new object gets id.
* Try to bulk-insert several rows to some table with ForeignKey to the newly
created object.

When I use the form without session

    cls.__table__.insert().execute(new_rows)

I got IntegrityError saying that the new object is not in DB.

When I use

    session.commit()
    cls.__table__.insert().execute(new_rows)

everything works as expected except I don't like this. One user action must be
one atomic DB transaction.


Thank you in advance,

Ladislav Lenart

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