sidorares / sidorares/node-mysql2

[Question] Automatic unescaping for column names and tables

Open
#1,192 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

I've recently posted a bug in the base library for mysql, but I'm not sure if this library shares the same problem:

https://github.com/mysqljs/mysql/issues/2391

When using mysql2 as our library, column names are incorrectly unescaped and reserved keywords conflict with column names like from, to, default, etc.

Am I doing something wrong when attempting to unescape the names in the queries?

Pasting the content of the original issue here for convenience:


Consider the following table:

CREATE TABLE example (
  `from` DATE NOT NULL
);

from is a reserved SQL keyword. If you try to insert a row into that table using named columns, the from column is not escaped, which causes a syntax error. I haven't found a way to force the library to not remove escapes for this column name even after forcing it to insert a raw value:

// this query unescapes "`from`" into "from"
connection.query(
  "INSERT INTO example (`from`) VALUES (?)",
  ["2020-01-01"]
);

// this query does the same
connection.query(
  "INSERT INTO example (?) VALUES (?)",
  [mysql.raw('`from`'), "2020-01-01"]
);

This also affects exports from MySQL that contain backtick-escaped column names that are reserved keywords when being passed to the query function after reading from a file.

Is there something I'm missing? Is there a way to force identifiers to always be escaped in the final query?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two connection.query examples using the reserved column name from, then compare the behavior with the linked mysqljs/mysql issue. Determine whether mysql2 provides a way to preserve identifier escaping and document the confirmed behavior or required change.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js, typescript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.