-
-
Notifications
You must be signed in to change notification settings - Fork 656
Open
Description
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,
}
});- 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]
);bondz
Metadata
Metadata
Assignees
Labels
No labels