The
documentation for the copy_from/copy_to cursor methods is
incorrect and misleading. These methods expect a buffer size as the
5th argument (not counting self) and a sequence of column names
only as the 6th argument.
I just wasted 3 hours trying to find out why the following
wouldn't work:
cursor.copy_from(
self.io,
self.table_name,
self.COLUMN_SEPARATOR,
self.NULL_LITERAL,
self.column_names
)
It kept failing with this error:
...
File "pg_copy.py", line 51, in copy_from
self.column_names
TypeError: an integer is required
Only when I went to the psycopg2 source code and started
learning about PyArg_ParseTupleAndKeywords and other
nasty Python internals it began to dawn on me that I was not insane
and a "size" argument needed to be passed before the "columns"
argument.
I'm now passing the latter as a keyword argument ...
cursor.copy_from(
self.io,
self.table_name,
self.COLUMN_SEPARATOR,
self.NULL_LITERAL,
columns = self.column_names
)
... but the documentation should be fixed to save others from
this ordeal.