Hi Mike,

When using Core, we can do a bulk insert and bulk return with Postgresql 
trivially:

    from sqlalchemy import table, column
    t = table('foo', column('id'), column('bar')

    ins = t.insert().values([{'bar': 'a'}, {'bar': 'b'}]).returning(foo.id)

    results = conn.execute(ins)
    
    ids = results.fetchall()

However, with raw textual SQL, it is a bit more inconvenient.

The following doesn't work:

    results = conn.execute(text(
        'insert into foo values (:bar) returning id
    ), [{'bar': 'a'}, {'bar': 'b'}])
    
    # raises sqlalchemy.exc.ResourceClosedError: This result object does 
not return rows. It has been closed automatically.
    results.fetchall()

To get it working, we have to do it this way:

    results = conn.execute(text(
        'insert into foo values (:bar0), (:bar1)
    ), {'bar0': 'x', 'bar1': 'y'})

    assert results.fetchall()

This isn't convenient. For example you would have to convert a list of bars 
like [{'bar': 'a'}, {'bar': 'b'}] into a single dict with uniquely name 
keys {'bar0': 'a', 'bar1': 'b'}.

I imagine sqlalchemy is doing that under the hood when using core. Is there 
some convenience function available in sqlalchemy core that I can use to 
simplify this?

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/ee82dcc7-7ff5-43e6-a405-1e5aedaaaeban%40googlegroups.com.

Reply via email to