MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.88k stars 399 forks source link

Table does not exist with `copy_to_table`, but it does #1029

Closed waldner closed 1 year ago

waldner commented 1 year ago

Minimal reproducer:

import asyncio
import asyncpg

async def create():
    conn = await asyncpg.connect('postgres://postgres:postgres@10.201.0.110:5433/postgres')
    script = ''' 
      DROP TABLE IF EXISTS public.foo;
      CREATE TABLE public.foo (id int, descr text);
    '''
    await conn.execute(script)

    await conn.copy_to_table(
        table_name='public.foo',
        source='/tmp/empty.csv',
        format='csv',
        delimiter='\t'
    )
    await conn.close()

if __name__ == '__main__':
    asyncio.run(create())

Result:

Traceback (most recent call last):
  File "/home/cz/asyncpg/minimal.py", line 20, in <module>
    asyncio.run(create())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/cz/asyncpg/minimal.py", line 12, in create
    await conn.copy_to_table(
  File "/home/cz/venv/lib/python3.10/site-packages/asyncpg/connection.py", line 896, in copy_to_table
    return await self._copy_in(copy_stmt, source, timeout)
  File "/home/cz/venv/lib/python3.10/site-packages/asyncpg/connection.py", line 1100, in _copy_in
    return await self._protocol.copy_in(
  File "asyncpg/protocol/protocol.pyx", line 529, in copy_in
asyncpg.exceptions.UndefinedTableError: relation "public.foo" does not exist

Since the table is indeed created, it seems that what's failing is copy_to_table. empty.csv is an empty file. It fails in the same way with a non-empty file.

elprans commented 1 year ago

The table name and the schema name should be passed as separate arguments, so copy_to_table(table_name='foo', schema_name='public').

waldner commented 1 year ago

That still fails with

Traceback (most recent call last):
...

  File "asyncpg/protocol/protocol.pyx", line 529, in copy_in
asyncpg.exceptions.UndefinedTableError: relation "public.public.foo" does not exist
waldner commented 1 year ago

Sorry, my bad. It works fine. Thanks!