Skip to content

sql tagged templates interpolates variable names, not values #1118

@iconara

Description

@iconara

This code ends up running SELECT DISTINCT columnName FROM my_table (it interpolates the name of the variable into the SQL):

const columnName = 'my_column'
const rows = await sql`SELECT DISTINCT ${columnName} FROM my_table`
// runs SELECT DISTINCT columnName FROM my_table

I expected it to run SELECT DISTINCT my_column FROM table (interpolate the value of columnName into the SQL). Variable interpolation seems to work this way in fenced sql blocks, and the following code also works:

const columnName = 'my_column'
const rows = await sql(['SELECT DISTINCT ' + columnName + ' FROM my_table'])
// runs SELECT DISTINCT my_column FROM my_table

but this, again, does not

const columnName = 'my_column'
const rows = await sql(['SELECT DISTINCT ', ' FROM my_table'], columnName)
// runs SELECT DISTINCT columnName FROM my_table

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn’t working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions