How do I use prepared statements in postgres?
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
Hi, I want to use prepared statements in a project. Therefor I am creating an statement and later use this one. However I am getting an error that the parameters/name does not match.
queryRunner.query(
`PREPARE MY_STATEMENT (text, text, timestamp with time zone, jsonb) as INSERT INTO DUMMY_TABLE (trip_id, vehicle_id, expiry_time, message) VALUES ($1, $2, $3, $4) ON CONFLICT (trip_id) DO UPDATE SET vehicle_id = $2, expiry_time = $3, message = $4`
);
then in the code I am using the same queryRunner like this:
queryRunner.query("EXECUTE MY_STATEMENT ($1, $2, $3, $4)", ["tripId", "v-id", moment(), null]);
When I am running the code I am getting the following error:
error: Query failed. error: bind message supplies 4 parameters, but prepared statement "" requires 0 - EXECUTE MY_STATEMENT ($1, $2, $3, $4)
(node:58125) UnhandledPromiseRejectionWarning: QueryFailedError: bind message supplies 4 parameters, but prepared statement "" requires 0
at new QueryFailedError (/node_modules/typeorm/error/QueryFailedError.js:11:28)
at Query.callback (node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
at Query.handleError (/node_modules/pg/lib/query.js:142:17)
at Connection.connectedErrorMessageHandler (/node_modules/pg/lib/client.js:211:17)
at Connection.emit (events.js:189:13)
at Socket.<anonymous> (node_modules/pg/lib/connection.js:126:12)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
I am using pg version 7.12.1
My server runs Postgres 10. Can someone please help me?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:5 (1 by maintainers)
Top GitHub Comments
const sql = 'SELECT * FROM table_name WHERE col_name = $1';
const result = await conn.manager.query(sql, [ param1 ]);
For stored procedures it gets weird. You’ll need to consult the postgres docs on this - we send everything over to postgres and it figures out what to do.
For other questions, please check out the community slack or check TypeORM’s documentation page on other support avenues - cheers!