Skip to content

[BUG] Named placeholders not properly replacing instances of nested placedholderΒ #4017

@JakeOrel

Description

@JakeOrel

Bug: namedPlaceholders fails to replace tokens inside nested subqueries

Environment

  • mysql2 version: 3.10.2
  • Node.js version: v22.21.1

Description

When using the namedPlaceholders: true option, the library fails to identify and replace named tokens (e.g., :item_id) when they are located inside a nested subquery. The same query executes successfully when using positional ? placeholders.

Steps to Reproduce

1. Failing Example (Named Placeholders)
The following code throws a syntax error because the second :item_id is sent literally to the MySQL server.

const item_id = 123;

const [results] = await pool.query({
  namedPlaceholders: true,
  sql: `
    SELECT *
    FROM my_table AS t1
    WHERE t1.id = :item_id
      AND EXISTS (
        SELECT 1 
        FROM table_2 AS t2
        WHERE t2.link_id = :item_id
        LIMIT 1
      )
  `,
  values: {
    item_id,
  }
});
  1. Working Example (Positional Placeholders) Using standard ? placeholders with the exact same SQL structure works as expected.
const item_id = 123;

const [results] = await pool.query(`
    SELECT *
    FROM my_table AS t1
    WHERE t1.id = ?
      AND EXISTS (
        SELECT 1 
        FROM table_2 AS t2
        WHERE t2.link_id = ?
        LIMIT 1
      )
  `, [item_id, item_id]
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions