question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Soremwarcommented, Feb 23, 2021

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:

const result = await client.queryObject(
  "INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, 'charleston')"
);
// Plain statement executed as is

const result = await client.queryObject(
  "INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, $1)",
  "charleston", // Query parameters
);
// Prepared statement, on execution time your database will understand that the value $1 maps to, is "charleston" in this case
const result = await client.queryObject(
  `INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, ${'charleston'})`,
  name,
);

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

INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, charleston)

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:

// Using template strings
const result = await client.queryObject`INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, ${'charleston'})`;

// Using plain text
const result = await client.queryObject(
  // Notice the extra quotes surrounding "charleston"
  `INSERT INTO acme.warehouses (warehouse_id, warehouse_name) VALUES (default, '${'charleston'}')`,
);
0reactions
tv42commented, Oct 15, 2021

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

  const result = await client.queryObject
    `SELECT ID, NAME FROM PEOPLE WHERE AGE > ${min} AND AGE < ${max}`;

as

  const result = await client.queryObject`
    SELECT ID, NAME FROM PEOPLE WHERE AGE > ${min} AND AGE < ${max}
  `;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found