Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insert multiple rows in one query #250

Closed
samuelcolvin opened this issue Jan 13, 2017 · 3 comments
Closed

insert multiple rows in one query #250

samuelcolvin opened this issue Jan 13, 2017 · 3 comments

Comments

@samuelcolvin
Copy link
Member

samuelcolvin commented Jan 13, 2017

Related to #112, I know executemany is not possible, but I wonder if it would be possible for aiopg (and aiopg-sqlachemy flavour) to support insert with multiple rows. As described here?

Currently, I'm getting

    await request['conn'].execute(q)
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/utils.py", line 72, in __await__
    resp = yield from self._coro
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/sa/connection.py", line 88, in _execute
    compiled_parameters = [compiled.construct_params(dp)]
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/sa/engine.py", line 23, in construct_params
    pd[column.key] = self._exec_default(column.default)
  File "/home/samuel/code/socket-server/env/lib/python3.6/site-packages/aiopg/sa/engine.py", line 31, in _exec_default
    return default.arg
AttributeError: 'Sequence' object has no attribute 'arg'

When trying a query like this.

@samuelcolvin
Copy link
Member Author

example query which would be great but currently gives the above error:

await conn.execute(
    sa_foo_table
    .insert()
    .values([
        dict(name='name1', x=1),
        dict(name='name2', x=2),
        dict(name='name3', x=3),
    ])
)

Surely this can't be impossible with psycopg2 as it's still a single query, just currently not possible with aiopg's sqlachemy integration.

@jettify
Copy link
Member

jettify commented Jan 13, 2017

Your approach should work, I do almost the same here
https://github.com/aio-libs/aiohttp_admin/blob/13289d0fc8bb3560dfbe08c1faa8d16093d9e903/demos/blog/aiohttpdemo_blog/generate_data.py#L65

@samuelcolvin
Copy link
Member Author

weird, wonder why I was getting an error before. Sorry.

Might be worth documenting this. I searched the docs for "bulk" and "executemany" and just found the code snippets saying "executemany isn't available".

Perhaps add to the docs "executemany isn't possible with aiopg, however you can still insert data on bulk with something like this [example]".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants