Trying to use prepared statements with python's psypcop2 for inserts needs to be setup manually. But using prepared statments with automatic columns such as serial id or timestamps causes errors.
Running sql statements directly via psql:
create table ps_test(
id serial primary key,
data text );
prepare pstmt (text) as insert into ps_test values( $1 );
execute pstmt( 'abc' );
execute pstmt( 'def' );
deallocate pstmt;
select * from ps_test;
gives "ERROR: column "id" is of type integer but expression is of type text".
Rewriting the table so that the automatic column is defined last:
drop table ps_test;
create table ps_test (
data text,
id serial primary key
);
prepare pstmt (text) as insert into ps_test values( $1 );
execute pstmt( 'abc' );
execute pstmt( 'def' );
deallocate pstmt;
select * from ps_test;
Works.
data | id
------+----
abc | 1
def | 2
(2 rows)
Is there a better way ?