Template strings and INSERT statements
See original GitHub issue- Deno Version:
1.7.4 - Deno-Postgres Version:
0.8.0 - Database:
CockroachDB
I’m trying to run an INSERT statement using a Pooled connection and template strings. The following is the query being ran:
const result = await client.queryObject(
`INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, ${'charleston'})`,
name,
);
Where ${'charleston'} comes from a variable and throws the following:
error: Uncaught (in promise) PostgresError: column "charleston" does not exist
return new PostgresError(parseWarning(msg));
^
at parseError (warning.ts:34:10)
at Connection.processError (connection.ts:613:19)
at Connection._simpleQuery (connection.ts:476:20)
at async Connection.query (connection.ts:712:16)
at async mod.ts:99:20
The following two queries do, however, work:
const result = await client.queryObject(
"INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, 'charleston')"
);
const result = await client.queryObject(
"INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, $1)",
"charleston",
);
I’ve created and referenced the connection as recommended by the docs.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >template strings in insert and update? · Issue #2044 - GitHub
hi, I have been struggling to make template strings work in insert and update queries and wondering if I am just doing it...
Read more >sql-template-strings - npm
ES6 tagged template strings for prepared statements with mysql and postgres. Latest version: 2.2.2, last published: 6 years ago.
Read more >Inserting if statement inside ES6 template literal - Stack Overflow
I was wondering if it it possible to insert an 'if' statement inside the template? Essentially to add a further line of code,...
Read more >Easy Creation of HTML with JavaScript's Template Strings
Let's look at an example where we need an if statement inside of our template string. This is taken straight from how you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi there @realStandal
I understand your confusion, however this is pretty simple to explain. You are mixing up two different concepts when executing a query: Template strings and function parameters
In the cases you described above, the following is executed by the driver:
This is actually a plain statement, cause you are not using query parameters or template strings, causing your query to be passed to PostgreSQL as the following
And since charleston is not surrounded by single quotes and doesn’t map to any query parameter, it’s though to be a database column rather than a literal value
In order to execute a query the way you intend it, means rewriting it into the following:
I just got stumped by this too.
Could I suggest changing the documentation at https://deno-postgres.com/#/?id=prepared-statement-with-template-strings to emphasize that it’s a tagged string literal, and not just backquotes.
I think the way the example code is displayed is absolutely hurting, making the tagged string harder to recognize. Could I recommend rewriting
as